MATLAB: How to Speed Up matlab for cycle

spped for loop rain flow matlab

im actually doing a code to use in a program based in the rain flow counting algorithm. The code has to read some huge vectors (ex:200000×1) and the cycle that im using is too slow. Could somebody help me to turn it faster, please???
A part of the code that im using is written below:
for i=1:N-1
if A(i+1) > A(i)
i=i+1;
maximo = A(i);
j= j + 1;
locais(j) = maximo;
elseif A(i+1) < A (i)
i=i+1;
minimo = A(i);
j= j + 1;
locais(j) = minimo;
end
end
locais=locais(:);
for k=1:j
B(k)=locais(k)';
end
B=B(:);
%%RAINFLOW METHOD
i=1;
for i=1:j
valores(i) = B(i);
aux(i)=valores(i);
i=i+1;
end
num=i;
d=1;
for d=1:N
i=1;
for i=1:j
valores(i)=aux(i);
end
k=length(valores);
for i=2:k-2
if valores(i-1)~=valores(i+1)
if valores(i-1) <= valores(i+1) && valores(i+1) < valores(i) ...
&& valores(i) <= valores(i+2)
guarda(i,1)=1;
S=abs(valores(i)-valores(i+1));
guarda(i,2)=S;
aux(i)=0;
aux(i+1)=0;
elseif valores(i-1) >= valores(i+1) && valores(i+1) > valores(i) ...
&& valores(i) >= valores(i+2)
guarda(i,1)=1;
S=abs(valores(i)-valores(i+1));
guarda(i,2)=S;
aux(i)=0;
aux(i+1)=0;
end
else
end
end
valores=[];
aux(find(aux==0)) = [];
j=length(aux);
d = d + 1;
end
Y_cf1=guarda(:,1);
Z_cf1=guarda(:,2);
Y_cf1=Y_cf1(Y_cf1~=0);
Z_cf1=Z_cf1(Z_cf1~=0);
Sc_cf1=[Y_cf1,Z_cf1] %Ciclos Completos
j=length(aux);
for i=1:j-1
if aux(i+1) > aux(i)
minimo = aux(i);
i = i + 1;
maximo = aux(i);
guarda_2(i,1)=0.5;
S2=abs(maximo-minimo);
guarda_2(i,2)=S2;
elseif aux(i+1) < aux(i)
maximo = aux(i);
i = i + 1;
minimo = aux(i);
guarda_2(i,1)=0.5;
S2=abs(maximo-minimo);
guarda_2(i,2)=S2;
else
end
end
Thanks in advance. Greetings Fagner Furtado

Best Answer

A)
ix=([logical(0);(diff(A(1:N))~=0)]);
L(find(id))=A(id);
locais=L(2:end);
I've got a meeting...gotta' run for now, sorry...
Undoubtedly can vectorize remaining fairly much, too.
One thing is it doesn't appear the for k=1:j B(k)=locais(k)'; end B=B(:);
loop does anything that
B=locais;
doesn't other than perhaps the column/vector orientation. I'd say create B to begin with and fix the orientation there as wanted/needed...
Related Question