Home > Mobile >  Add constant values to a column in a table - Matlab
Add constant values to a column in a table - Matlab

Time:09-17

I wish to add into a table the values of x2 and y2. As both x2 and y2 have a constant values I just wrote x2 value 10 times. Is there a short way to do so which can also be applied on y2 value column?

code:

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)

CodePudding user response:

You can either use 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);

Or if you have an existing table, you can assign a constant to an entire column like so

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
  • Related