如何将一个表列复制到另一个关于行名称?

时间:2021-04-25 19:37:25

In the following toy example, tables t1 and t2 have shapes (3 x 0) and (3 x 1), respectively. Furthermore, both tables have the same row names.

在下面的玩具示例中,表t1和t2分别具有形状(3×0)和(3×1)。此外,两个表都具有相同的行名称。

>> t1 = table('RowNames', {'a', 'b', 'c'});
>> t2 = table([3 ; 2 ; 1], ...
              'RowNames', {'c', 'a', 'b'}, 'VariableNames', {'x'});

Then a copy of t2's single column is added to t1 as a new column, with the same variable name.

然后将t2的单个列的副本添加到t1作为新列,具有相同的变量名称。

>> t1.('x') = t2.('x');

The resulting table t1, however, differs from t2 in the association between row names and the values in the x-column:

但是,结果表t1与行名称和x列中的值之间的关联中的t2不同:

>> t1({'a', 'b', 'c'}, :)
ans =
         x
         _
    a    3
    b    2
    c    1
>> t2({'a', 'b', 'c'}, :)
ans =
         x
         _
    a    2
    b    1
    c    3

What's the simplest way to assign t2.('x') to t1.('x') "respecting rownames"? By this last condition I mean that the final t1 should look just like t2; e.g.:

将t2。('x')分配给t1。('x')“尊重rownames”的最简单方法是什么?通过这最后一个条件,我的意思是最终的t1看起来应该像t2;例如。:

>> t1({'a', 'b', 'c'}, :)
ans =
         x
         _
    a    2
    b    1
    c    3

3 个解决方案

#1


You can index the table using row names so if you extract the list of rownames from t1 you can use that as the ordering for t2:

您可以使用行名称对表进行索引,因此如果从t1中提取rownames列表,则可以将其用作t2的顺序:

order = t1.Properties.RowNames % cell array
intermediate = t2(order, :);

or just do it all in one go:

或者只是一气呵成:

t2(t1.Properties.RowNames, :);

#2


Since t1 doesn't have the x column you can concatenate t1 with column x of t2

由于t1没有x列,因此可以将t1与t2的x列连接起来

>> t1=[t1, t2(:,'x')]
t1 = 
         x
         _
    a    2
    b    1
    c    3

It will automatically take care of matching rows.

它会自动处理匹配的行。

#3


OK, this is the OP here.

好的,这是OP。

I found a (potential) answer to my question: instead of

我发现了一个(潜在的)我的问题的答案:而不是

t1.('x') = t2.('x');

use

t1.('x') = t2{t1.Properties.RowNames, 'x'};

I say that this is a "potential" answer because with MATLAB I never know when something that works for a particular type, or under particular circumstances, will generalize. E.g., at this point I don't know if the above will work if column x holds non-numeric values.

我说这是一个“潜在的”答案,因为在MATLAB中,我永远不知道何时适用于特定类型或特定情况的东西会概括。例如,此时我不知道如果列x包含非数字值,上述内容是否有效。

If someone knows of a better way, or can point to documentation in support of my lucky guess here, please post it. I'll be glad to accept it as the answer.

如果有人知道更好的方法,或者可以指出支持我的幸运猜测的文档,请发布。我很乐意接受它作为答案。

#1


You can index the table using row names so if you extract the list of rownames from t1 you can use that as the ordering for t2:

您可以使用行名称对表进行索引,因此如果从t1中提取rownames列表,则可以将其用作t2的顺序:

order = t1.Properties.RowNames % cell array
intermediate = t2(order, :);

or just do it all in one go:

或者只是一气呵成:

t2(t1.Properties.RowNames, :);

#2


Since t1 doesn't have the x column you can concatenate t1 with column x of t2

由于t1没有x列,因此可以将t1与t2的x列连接起来

>> t1=[t1, t2(:,'x')]
t1 = 
         x
         _
    a    2
    b    1
    c    3

It will automatically take care of matching rows.

它会自动处理匹配的行。

#3


OK, this is the OP here.

好的,这是OP。

I found a (potential) answer to my question: instead of

我发现了一个(潜在的)我的问题的答案:而不是

t1.('x') = t2.('x');

use

t1.('x') = t2{t1.Properties.RowNames, 'x'};

I say that this is a "potential" answer because with MATLAB I never know when something that works for a particular type, or under particular circumstances, will generalize. E.g., at this point I don't know if the above will work if column x holds non-numeric values.

我说这是一个“潜在的”答案,因为在MATLAB中,我永远不知道何时适用于特定类型或特定情况的东西会概括。例如,此时我不知道如果列x包含非数字值,上述内容是否有效。

If someone knows of a better way, or can point to documentation in support of my lucky guess here, please post it. I'll be glad to accept it as the answer.

如果有人知道更好的方法,或者可以指出支持我的幸运猜测的文档,请发布。我很乐意接受它作为答案。