필터 지우기
필터 지우기

Loop output not saving to a cell array. Only last solution saving to cell

조회 수: 4 (최근 30일)
EM
EM 2018년 6월 28일
댓글: EM 2018년 6월 28일
This question has been asked and I have read the accepted answers. But I just don't get it. Its been several hours working on this now and I'm hoping for some very clear help.
I have a loop with only 6 iterations. Within that loop is a function. Each time the loop runs, the function solves and a solution array of size (20000, 1) is generated. I want to save each solution array for all 6 iterations. But my attempts have resulted in only the last iteration's solution being stored for all cnew{1,1}, cnew{2,1} ... cnew{6,1}. They're all equal in the cnew cell, but I know with 100% certainty that each solution array z is not equal with changing parameter values.
Please help
Below is my code:
for iter=1:6
if iter==1, y=0; end
if iter==2, y=0.15; end
if iter==3, y=0.30; end
if iter==4, y=0.45; end
if iter==5, y=0.60; end
if iter==6, y=0.75; end
z = function( _various inputs that change with y_);
cnew = cell(6,1);
for loopiter = 1:6
cnew{iter}=z;
end
end

채택된 답변

Stephen23
Stephen23 2018년 6월 28일
편집: Stephen23 2018년 6월 28일
You only need one loop:
vec = 0:0.15:0.75;
cnew = cell(numel(vec),1);
for iter = 1:numel(vec)
y = vec(iter);
cnew{iter} = function( _various inputs that change with y_);
end
  댓글 수: 16
Stephen23
Stephen23 2018년 6월 28일
편집: Stephen23 2018년 6월 28일
@EM: The problem with your original code was the two loops:
for iter = 1:6
if iter==1, y=0; end
if iter==2, y=0.15; end
if iter==3, y=0.30; end
if iter==4, y=0.45; end
if iter==5, y=0.60; end
if iter==6, y=0.75; end
z = function( _various inputs that change with y_);
cnew = cell(6,1);
for loopiter = 1:6
cnew{iter}=z;
end
end
Note that the on every iteration of the outper loop you completely redefine cnew, so anything stored in cnew on iterations 1:5 is discarded. That leaves only the final iteration, where y=0.75: on this final iteration you calculate z, redefine cnew again (discarding any existing data), and then use the inner loop to put the same z value into each cell of cnew. Thus cnew only contains the z value from the final outer loop iteration.
Using a breakpoint was a very good idea: step through until the end and you will notice that you redefine cnew on each outer loop iteration, discarding all of the existing data. You will also notice that the inner loop allocates exactly the same z value to all cnew cells.
My answer has one loop, iterating as many times as there are elements in vec. For each element it gets one y value from the vector, calculates the function, and saves the output value in cnew.
EM
EM 2018년 6월 28일
Great, that makes sense. Thank you for the explanation. Please save this explanation for any future visitors

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by