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

조회 수: 2 (최근 30일)
T Shep
T Shep 2016년 12월 1일
편집: Alexandra Harkai 2016년 12월 1일
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

채택된 답변

Alexandra Harkai
Alexandra Harkai 2016년 12월 1일
편집: Alexandra Harkai 2016년 12월 1일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by