Matlab 从另一个具有不同维度的 table 添加行到 table

Matlab adding rows to a table from another table with different dimensions

我有一个尺寸为 6x2 的 table。我还有其他尺寸为 4x1 的 table,我试图将较小的 table 附加到较大的 table,但由于尺寸不同我无法做到。您对解决这个问题有什么建议吗?

代码示例:

id = {'AB';'ZX';'DF4';'CA';'AC';'FG'}
mean = [1.5;2;3;1.15;3.06;1]
table1 = table(id,mean);

id2 = {'ZZ';'ZB';'FG';'4FA'};
table2 = table(id2);
table2.Properties.VariableNames = {'id'};

然后我将使用前 6 行的平均值计算最后 4 行的平均值。

首先,这非常重要:不要调用变量 "mean",因为这是 MATLAB 中的平均函数。

我建议您先计算它们,然后连接表格,但如果您希望这样也可以。您只需填写一些内容,直到获得实际值

id = {'AB';'ZX';'DF4';'CA';'AC';'FG'};
mean_values = [1.5;2;3;1.15;3.06;1]; %way better than 'mean'
table1 = table(id,mean_values);

id2 = {'ZZ';'ZB';'FG';'4FA'};
table2 = table(id2);
table2.Properties.VariableNames = {'id'};
%add some sort of data like NaN 
table3=[table1;[table2 table(nan(height(table2),1),'Variablenames',{'mean_values'})]];

您也可以使用 0、1 或任何其他数字,但它们可能会影响您对平均值的计算,而 NaN 不会:

mean([3 5 nan],'omitnan')

如果你在使用 mean saying 'Subscript indices must either be real positive integers or logicals.' 时遇到错误,你必须使用

clear mean

或从工作区手动删除它