How to use For loop

1 view (last 30 days)
Emma McFerrier
Emma McFerrier on 18 May 2021
Answered: Arthi Sathyamurthi on 24 May 2021
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

Answers (1)

Arthi Sathyamurthi
Arthi Sathyamurthi on 24 May 2021
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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by