Recording values from a loop

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.

Réponses (1)

Kevin Phung
Kevin Phung le 11 Fév 2019
Modifié(e) : Kevin Phung le 11 Fév 2019

0 votes

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 commentaires

Alex Brown
Alex Brown le 11 Fév 2019
Modifié(e) : Alex Brown le 11 Fév 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 le 11 Fév 2019
Modifié(e) : Kevin Phung le 11 Fév 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!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 11 Fév 2019

Modifié(e) :

le 11 Fév 2019

Community Treasure Hunt

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

Start Hunting!

Translated by