Optimize an if loop

1 Ansicht (letzte 30 Tage)
Ricardo López
Ricardo López am 8 Nov. 2020
Kommentiert: Ricardo López am 9 Nov. 2020
Good night people,
I have a question regarding the optimization of the following loop:
for i=1:length(PV)
if i>3
if NL(i-1)<=100
if NL(i-2)<=100
if NL(i-3)<=100
u(i)=1;
else
u(i)=0;
end
end
end
end
end
I would like to reduce the if statements.
Thanks for your time,

Akzeptierte Antwort

Matt J
Matt J am 8 Nov. 2020
t=double(NL(:).'<=100);
u=(movsum(t,[3,0])-t)>=3;
  1 Kommentar
Ricardo López
Ricardo López am 9 Nov. 2020
What should I do if I want to add more conditions, like in my example? And stored them in a u=zero(1,length(PV))
Thank you for your time

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 8 Nov. 2020
Bearbeitet: Matt J am 8 Nov. 2020
u=[0,0,0, conv(double(NL(:).'<=100),[1,1,1],'valid')>=3];

Kategorien

Mehr zu Loops and Conditional Statements 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