How to Speed Up matlab for cycle?

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:200000x1) 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

 Accepted 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...

3 Comments

Thanks again. I have made some changes in the code like you said but there's still a part of the code there's too slow and i don't know how to make it faster.
That is the part:
valores = (rand(200000,1)*100);
N=length(valores);
d=1;
for d=1:N;
valores=aux;
for i=2:N-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
end
end
valores=[];
aux(find(aux==0)) = [];
j=length(aux);
d = d + 1;
end
Once more thanks in advance. Greetings,
Fagner Furtado
I'll try to take a look here later on today...got to get stuff done outside this morning before it gets too hot.
If you don't mind, I'll post on newsgroup instead of here going forward--I'll use your subject line under dpb. It's just so klunky to try to write anything of any significance in this forum interface I just can't stand it... :)
I know TMW wants to push Answers forum 'cuz they can control it but until they can get a usable newsreader-like interface I think I'm going to retire again--I just don't have the patience to put up w/ the excessive hooplas to use it. :(
I don't know, TMW, can't you just mirror to/from cs-sm postings?
My bad...earlier this AM I mistakenly got the posting into the Fortran group instead of cs-sm--it's there now

Sign in to comment.

More Answers (1)

OK, since the same thing is done under both branches of the if...else construct, one can replace the loop by logical addressing
f1=[0; (v(3:end-1)~=v(1:end-3))]'; % first if logical vector
% The if and else conditions...
f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)]';
f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)]';
% so, the clauses are executed if f1 and either f2 or f3...
f=f1&(f2|f3);
% then
g(f,1)=1;
g(f,2)=abs(diff(v(f)));
I've not had time to test but that appears to be ok methinks...
Can you explain the role of d? One poster in clf had a routine in Fortran that appears to be the algorithm and says the d is a residual but there aren't a lot of comments and those there are are in French which I don't read well enough to follow, unfortunately.
Or, a link to the reference would be good...
Again, I've posted the above in a thread in the newsgroup where it's much easier to interact if you want to follow up further.

Categories

Community Treasure Hunt

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

Start Hunting!