How do you start a new column for every iteration in a loop?

5 vues (au cours des 30 derniers jours)
T Shep
T Shep le 1 Déc 2016
I am loading trying to store each iteration in a separate column in: percent(j,:)=[percent_correct].
There should be a total of 11 columns with 50 rows. However, only the last 50 values are stored.
sid = 'data_x';
for ii = 1:11
load([sid,num2str(ii),'.mat'])
for j = 1:50
num_words= numel(q.data.sent{j})
num_correct=data(j,4)
percent_correct=num_correct/num_words
percent(j,:)=[percent_correct]
end
end

Réponse acceptée

Alexandra Harkai
Alexandra Harkai le 1 Déc 2016
Modifié(e) : Alexandra Harkai le 1 Déc 2016
If data is a 2-dimensional array then percent_correct is a scalar so you need to make sure it is saved in one exact place in the percent array.
Also it helps if you preallocate the matrix before the loop:
rows = 50;
cols = 11;
percent = zeros(rows, cols);
for ii = 1:cols
...
for j = 1:rows
...
percent(j, ii)=[percent_correct];
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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