필터 지우기
필터 지우기

How do you save while loop output in a vector?

조회 수: 2 (최근 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 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