How do you save while loop output in a vector?

1 次查看(过去 30 天)
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
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 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