Recording values from a loop

6 visualizaciones (últimos 30 días)
Alex Brown
Alex Brown el 11 de Feb. de 2019
Editada: Kevin Phung el 11 de Feb. de 2019
I am using a nested for loop to create an index of calculated values. I need it so everytime a value of T is calculated to be reported in order for the first iteration of the loop to the end. In this format:
where beds is the number of loops, n=beds
T1 T2 T3 ... Tn
1 2 3 ... n
Maybe this is really simple but I cannot get it to work, here's what I've tried
(Matlab R2016b)
while
%Calculations
for z=1:beds;
temps(z)=(T);
end
%Misc code
beds=beds+1;
end
returns 126 of the same T value as Tn
this code just run each temps off as the same value in a 1x126 double and this is not intended.

Respuestas (1)

Kevin Phung
Kevin Phung el 11 de Feb. de 2019
Editada: Kevin Phung el 11 de Feb. de 2019
You're missing indexing for your T
while
%Calculations
for z=1:beds;
temps(z)=T(z);
end
%Misc code
beds=beds+1;
end
however.. it looks like youre just setting temps to be equal to T-- do you even need the forloop?
  2 comentarios
Alex Brown
Alex Brown el 11 de Feb. de 2019
Editada: Alex Brown el 11 de Feb. de 2019
Solution I found:
beds = 1; %% ORIGINALLY 0 SO CODE DIDN'T WORK!!!
while %Condition
z=beds;
%Calculations
temps(z)=[T]
beds=beds+1;
end
beds = beds - 1;
NB : I don't think z=beds is required, temps(beds) should work.
Kevin Phung
Kevin Phung el 11 de Feb. de 2019
Editada: Kevin Phung el 11 de Feb. de 2019
'NB : I don't think z=beds is required, temps(beds) should work.'
yep, it will save you some space and time, however insignificant. Glad you caught your error.
for future reference though, do try to provide as much information as possible so it'll be easier to answer your question!

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