How to use For loop

조회 수: 1 (최근 30일)
Emma McFerrier
Emma McFerrier 2021년 5월 18일
답변: Arthi Sathyamurthi 2021년 5월 24일
Hi, I am doing a geology assignment and would love some assistance with the for loop function. I have to refine the ages of some terraces and to do that I need to do a for loop where I tell Matlab that the fan is younger than T5 and T3 to get an output age that follows stratagraphic order. So far this is what I have: ageT3=normrnd(16500,4850,10000,1) ageT5=normrnd(14100,4600,10000,1) ageFan=normrnd(9850,1750,10000,1) for i=1:10000 T3(i)=datasample(ageT3,1); T5(i)=datasample(ageT5,1); Fan(i)=datasample(ageFan,1); if Fan(i)<T5(i)<T3(i) Ages(i)=unifrnd(Fan(i),T3(i) else Ages(i)=NaN; end end
I hope this makes sense.I dont use Matlab often and I have no idea what I am doing.
Thanks

답변 (1개)

Arthi Sathyamurthi
Arthi Sathyamurthi 2021년 5월 24일
Expressions like a<b<c are interpreted as (a<b)<c which turns out to compare the logical result from a<b (either 0 or 1) with c. To test if the value variable fan is less than T3 and T5 use the condition as (Fan(i)<T5(i)) && (Fan(i)<T3(i))
Your snippet would be like,
for i=1:10000
T3(i)=datasample(ageT3,1);
T5(i)=datasample(ageT5,1);
Fan(i)=datasample(ageFan,1);
if (Fan(i)<T5(i)) && (Fain(i)<T3(i))
Ages(i)=unifrnd(Fan(i),T3(i))
else
Ages(i)=NaN;
end
end
You can look at the Mathworks Documentation pages for the for loop and if loop for further understanding.

카테고리

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