How can I save the results of a nested loop?

조회 수: 4 (최근 30일)
Shahpour Turkian
Shahpour Turkian 2022년 9월 9일
댓글: Shahpour Turkian 2022년 9월 9일
I have two input matrices: qq and om
l = [0 0.05 0.1 0.15];
qq = perms(l).';
m = [0.01 0.001 0.0001 0.0002];
om = perms(m);
These inputs get fed in my Function which in return will output a 1-by4 output, so I created a 24-by-4 results
results = zeros(width(om), length(om)*length(qq));
for i=1:length(om)
for j=1:length(qq)
results(i,j) = Function(qq(:,j),om(i,:))
end
end
since the output of my function is 1-by-4, I can't save it in a 1-by-1 cell so I get:
I get: Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-4.
I also tried
[results(i,1) results(i,2) results(i,3) results(i,4)] = AA_BL_M(qq(:,j),om(i,:))
this seems to work but doesn't save the iterations:
results = AA_BL_M(qq(:,j),om(i,:))

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 9월 9일
Note that your preallocation of results does not match the number of times your for loops will run. Keeping your for loops the way they are, your resutls variable will need to be of size (i,j,4) to capture your results (or a (i,j) cell array)
You could try this
results = zeros(length(om), length(qq),4);
...
results(i,j,:) = Function(qq(:,j),om(i,:))
or
results = cell(length(om), length(qq));
...
results{i,j} = Function(qq(:,j),om(i,:))

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