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

185 次查看(过去 30 天)
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

采纳的回答

Adam Danz
Adam Danz 2019-6-20
编辑:Adam Danz 2019-11-21
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by