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

2 ビュー (過去 30 日間)
Mohammed
Mohammed 2014 年 9 月 22 日
コメント済み: Roger Stafford 2014 年 9 月 22 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by