将常量值添加到 table 中的列 - Matlab

Add constant values to a column in a table - Matlab

我想将 x2 和 y2 的值​​添加到 table 中。由于 x2 和 y2 都有一个常数值,我只写了 10 次 x2 值。有没有一种可以应用于 y2 值列的简短方法?

代码:

clc;
clear all;
x1 = [38;43;38;40;49;18;41;58;10;55];
y1 =rot90(11:20);

x2 =[2;2;2;2;2;2;2;2;2;2];
y2 =6;
 
dTable = table(x1,y1,x2,y2)

您可以使用 repmat

x1 = [38;43;38;40;49;18;41;58;10;55];
x2 = repmat( 2, 10, 1 ); % 10 rows, 1 column
dTable = table(x1,x2);

或者如果您有一个现有的 table,您可以像这样为整个列分配一个常量

x1 = [38;43;38;40;49;18;41;58;10;55];
dTable = table(x1);
dTable.x2(:) = 2; % Assign all rows of column "x2" to the value 2