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

1 次查看(过去 30 天)
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 个评论
Roger Stafford
Roger Stafford 2014-9-22
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'.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by