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

2 views (last 30 days)
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

Accepted Answer

Alexandra Harkai
Alexandra Harkai on 1 Dec 2016
Edited: Alexandra Harkai on 1 Dec 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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!