Borrar filtros
Borrar filtros

How do you save while loop output in a vector?

1 visualización (últimos 30 días)
Lilian Fierro Arcos
Lilian Fierro Arcos el 4 de Sept. de 2015
Comentada: Thorsten el 8 de Sept. de 2015
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

Respuestas (1)

Thorsten
Thorsten el 4 de Sept. de 2015
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 comentarios
Lilian Fierro Arcos
Lilian Fierro Arcos el 5 de Sept. de 2015
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 el 8 de Sept. de 2015
Please post a complete example that we can run to have closer look at what goes wrong.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by