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

185 ビュー (過去 30 日間)
Julia Williams
Julia Williams 2019 年 6 月 20 日
編集済み: Adam Danz 2019 年 11 月 21 日
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 件のコメント
Matthieu Sherwood
Matthieu Sherwood 2019 年 11 月 21 日
Hi adam!
Adam Danz
Adam Danz 2019 年 11 月 21 日
You found me! :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by