How to avoid for nested loops with if condition?
古いコメントを表示
Hello,
I have big matrixes of same size, and am suing for loop with if statement, which is bottleneck in my code and is very slow. How would it be possible to optimize it?
for i=1:n1
for j=1:n2
if id(i,j)==1
if RR(i,j)==1
id(i,x(i,j))=0;
end
end
end
end
Maybe it is possible to vectorize or use bsxfun?
採用された回答
その他の回答 (1 件)
Nithin Banka
2018 年 6 月 25 日
I don't think you need to use 'for' loop. The 2 'if' statements can be easily handled in MATLAB.
index_of_1_in_id_and_RR = (id==1&RR==1); %valid as you told they have same size
You can use this variable in further steps.
3 件のコメント
Mantas Vaitonis
2018 年 6 月 25 日
Nithin Banka
2018 年 6 月 25 日
Can you provide me 'x'?
Mantas Vaitonis
2018 年 6 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!