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

186 vues (au cours des 30 derniers jours)
Julia Williams
Julia Williams le 20 Juin 2019
Modifié(e) : Adam Danz le 21 Nov 2019
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

Réponse acceptée

Adam Danz
Adam Danz le 20 Juin 2019
Modifié(e) : Adam Danz le 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 commentaires
Matthieu Sherwood
Matthieu Sherwood le 21 Nov 2019
Hi adam!
Adam Danz
Adam Danz le 21 Nov 2019
You found me! :D

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by