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

2 views (last 30 days)
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 Comment
Roger Stafford
Roger Stafford on 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'.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!