How to replace a column in a table with different values?

188 views (last 30 days)
aa is a 1338 x 133 double. The first part of this code averages the data to be 13 x 133. However, I do not want the first column to be averaged. I want it to read 1,2,3,4,5,6,7,8,9,10,11,12,13. I tried to accomplish this by transforming the double into a table and replacing the first column with the numbers I want but I run into this error. Any help on either the first part or the second part of this code would be much appreciated!
Code:
x = aa;
p = 100;
n = size(x, 1); % Length of first dimension
nc = n - mod(n, p); % Multiple of p
np = nc / p; % Length of result
xx = reshape(x(1:nc, :), p, np, []); % [p x np x size(x,2)]
y = sum(xx, 1) / p; % Mean over 1st dim
y = reshape(y, np, []); % Remove leading dim of length 1
B = array2table(y);
new = ['1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13']; % the column to replace column 1 with
B(:, 1)= new; % the column to replace column 1 with
Error:
Error using june20code (line 13)
To assign to or create a variable in a table, the number of rows must match the height of the table

Accepted Answer

Adam Danz
Adam Danz on 20 Jun 2019
Edited: Adam Danz on 21 Nov 2019
You were close. Use curly brakets and input a column vector.
B{:,1} = (1:13).';
% or
B.y1 = (1:13).';
Note that the examples above use numbers. If you wanted to use strings or char arrays (as in your example),
new = {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13'}';
B.y1 = new';
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!