How do you save while loop output in a vector?

3 ビュー (過去 30 日間)
Lilian Fierro Arcos
Lilian Fierro Arcos 2015 年 9 月 4 日
コメント済み: Thorsten 2015 年 9 月 8 日
Hi, I'm new to Matlab, I have just recently started learning it and I am quite puzzle about how to save the output of my while loop in a vector so I can plot it later. I think my problem is that I am using two subscripts in the loop ( i and t). The loop works fine and it is giving me the right information, but I cannot save its output. I have searched the web and matlab answers, but nothing seems to work in my case. Anyway, this is the loop:
Hc=[];
Fc=[];
while (round(H(t),1)~=round(H(t-1),1)&&round(F(t),1)~=round(F(t-1),1));
t=t+1;
i=t-1;
H(t)=H(i)+L*(N(i)/(w+N(i)))-H(i)*(0.25-0.75*(F(i)/(N(i)+0.001)));
F(t)=F(i)+H(i)*(0.25-0.75*(F(i)/(N(i)+0.001)))-m*F(i);
N(t)=H(t)+F(t);
Hc(t)=[H(t)];
Fc(t)=[F(t)];
end
I've created Hc and Fc as vectors to store the information from H(t) and F(t), but it does not seem to store any data. Do you have any ideas on how this could be fixed?
Thanks,
Denisse

回答 (1 件)

Thorsten
Thorsten 2015 年 9 月 4 日
You don't have to introduce Hc or Fc; the data are already stored in F and H, as in the following example
t = 1;
while t < 10
F(t) = t^2;
t = t+ 1;
end
If Hc and Fc are empty that is probably because the while loop has never been entered, because the expression
(round(H(t),1)~=round(H(t-1),1)&&round(F(t),1)~=round(F(t-1),1));
was never true.
  2 件のコメント
Lilian Fierro Arcos
Lilian Fierro Arcos 2015 年 9 月 5 日
I am using two variables: t and i, basically because I need t>i by 1. I tried taking out Hc and Fc, but it still does not store my variables. But they are working because if I ran the function without ";" at the end, it is producing data and I do get the correct final outputs. So I am still confused about what I am doing wrong.
Thorsten
Thorsten 2015 年 9 月 8 日
Please post a complete example that we can run to have closer look at what goes wrong.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by