How to use looping

1 Ansicht (letzte 30 Tage)
Skydriver
Skydriver am 27 Nov. 2019
Kommentiert: Skydriver am 2 Dez. 2019
I have a coding like this:
for k=1:length(Depth)
if Depth(k)>0 & Depth(k)<=6
RdFk(k) = -0.0425 .*Depth(k)+1.0333
elseif Depth(k)>6 & Depth(k)<12
RdFk(k) = 0.0006 .*(Depth(k))^2 + 0.0048.*(Depth(k))+0.7865
elseif Depth(k)>=12 & Depth(k)<=15
RdFk(k) = -0.02.*Depth(k) + 1.06
else Depth(k)>16 & Depth(k)<=30
RdFk(k) = -0.0025.*(Depth(k))^2+0.072.*(Depth(k))+0.261
end
end
Why the coding only read the last else (last criteria i.e. RdFk(k) = -0.0025.*(Depth(k))^2+0.072.*(Depth(k))+0.261 ) and pass through other criteria?
Thx

Akzeptierte Antwort

David Hill
David Hill am 27 Nov. 2019
You might want to eliminate your loop.
RdFk=zeros(size(Depth));
RdFk(Depth>0&Depth<=6) = -0.0425*Depth(Depth>0&Depth<=6)+1.0333;
RdFk(Depth>6&Depth<=12) = 0.0006*(Depth(Depth>6&Depth<=12)).^2 + 0.0048*(Depth(Depth>6&Depth<=12))+0.7865;
RdFk(Depth>12&Depth<=15) = -0.02*Depth(Depth>12&Depth<=15) + 1.06;
RdFk(Depth>15&Depth<=30) = -0.0025*(Depth(Depth>15&Depth<=30)).^2+0.072*(Depth(Depth>15&Depth<=30))+0.261;
  1 Kommentar
Skydriver
Skydriver am 2 Dez. 2019
I change the zero with ones and it works now
Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by