how to save the value of each iteration in a loop for later use in the program?

My code is:
a=input; b=input;
while(1)
c=statement;
if(x>0)
s=a-c;
a=altered a;
else
break;
end
end
I want each value of s to be known for later use.

回答 (2 件)

Julia
Julia 2015 年 4 月 10 日

0 投票

Hi,
use an additional counter and an array/vector for s. s will change its size in every iteration and Matlab will show you a warning. If you have an upper bound for the loop iterations I suggest to use it for preallocation.

3 件のコメント

Aru hazari
Aru hazari 2015 年 4 月 10 日
Could you please help me out with an example. Thank you :)
khan
khan 2015 年 4 月 10 日
initialize counter_ind =1; before while loop. modify your statement to this
s(counter_ind) = a-c;
counter_ind = counter_ind+1;
Aru hazari
Aru hazari 2015 年 4 月 10 日
Thank you Khan but i get an error for mismatch in dimentions when i do that.

サインインしてコメントする。

khan
khan 2015 年 4 月 10 日

0 投票

may be your data in a-c is bigger than a value. So you have to use according to your data i.e. a and b. i dont know exactly what do you have in a and c. but i think this will save anything.
s{counter_ind} = a-b;

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2015 年 4 月 10 日

回答済み:

2015 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by