Saving data from loop

조회 수: 4 (최근 30일)
hi  hey
hi hey 2018년 1월 21일
댓글: hi hey 2018년 1월 21일
while(run<time)
if(run > time)
break
end
~do calculations~
z=[];
pie= crust*apple;
z=[pie];
~do calculations~
end
I'm trying to store all of the calculations from pie but for some reason it is being overwritten each time it goes through the loop. what else would I need to add to have the data saved correctly?

채택된 답변

Stephen23
Stephen23 2018년 1월 21일
편집: Stephen23 2018년 1월 21일
method one: concatentation:
z = [];
while ...
...
pie = ...
z = [z,pie];
end
method two: indexing:
replace z = [z,pie] with:
z(end+1) = pie;
Note that expanding z on each iteration will be very inefficient, and will likely slow down your code: it is rarely a good idea to start with an empty array and enlarge it on each loop iteration. See:
  댓글 수: 1
hi  hey
hi hey 2018년 1월 21일
It works! Thank you very much.

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

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