Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Mohammed
Mohammed am 22 Sep. 2014
Kommentiert: Roger Stafford am 22 Sep. 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 Kommentar
Roger Stafford
Roger Stafford am 22 Sep. 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'.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by