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

조회 수: 224 (최근 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
Adam Danz
Adam Danz 2019년 11월 21일
You found me! :D

댓글을 달려면 로그인하십시오.

추가 답변 (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