Can you help me improve the code & speed of the nested for loop?

2 visualizaciones (últimos 30 días)
Mohammed
Mohammed el 22 de Sept. de 2014
Comentada: Roger Stafford el 22 de Sept. de 2014
I wrote this part of the code this afternoon, it works fine but don't like it, any other options to write it a better way.
SValAgree = zeros(lngPSCset,2);
for i=1:lngPSCset
match = 0;
for j=3:clmPSCset-3
for k=6:clmPSCset
if PSCset(i,j) ~= 0 && PSCset(i,j) == PSCset(i,k)
match = match + 1; break;
else continue
end
end
end
ASValAgree(i,1) = match;
end
Thanks!!
  1 comentario
Roger Stafford
Roger Stafford el 22 de Sept. de 2014
Are you sure this code is doing you what you think it is? When j becomes 6 or greater in the second loop and PSCset(i,j) is nonzero, then a match is inevitable at the k which equals j, so there is no need to do all that comparing in the inner k-loop. For such a j, the code will always add 1 to 'match'.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by