How to use For loop

2 次查看(过去 30 天)
Emma McFerrier
Emma McFerrier 2021-5-18
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