Recording values from a loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
답변 (1개)
Kevin Phung
2019년 2월 11일
편집: Kevin Phung
2019년 2월 11일
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
Kevin Phung
2019년 2월 11일
편집: Kevin Phung
2019년 2월 11일
'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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!