Clear Filters
Clear Filters

How to use looping

3 views (last 30 days)
Skydriver
Skydriver on 27 Nov 2019
Commented: Skydriver on 2 Dec 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

Accepted Answer

David Hill
David Hill on 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 Comment
Skydriver
Skydriver on 2 Dec 2019
I change the zero with ones and it works now
Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!