Borrar filtros
Borrar filtros

How to use looping

3 visualizaciones (últimos 30 días)
Skydriver
Skydriver el 27 de Nov. de 2019
Comentada: Skydriver el 2 de Dic. de 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

Respuesta aceptada

David Hill
David Hill el 27 de Nov. de 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 comentario
Skydriver
Skydriver el 2 de Dic. de 2019
I change the zero with ones and it works now
Thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by