How do I Vectorize the for loop containing if else ?

for j=1:n
for i=1:m
if xH(i,j)>0
X1(i,j)=xH(i,j);
X2(i,j)=xH(i,j);
end
end
end
for j=1:n
COUNTcol=0;
for i=1:m
if X1(i,j)>0
COUNTcol=COUNTcol+1;
end
end

 採用された回答

Voss
Voss 2022 年 12 月 27 日

0 投票

X1 = zeros(size(xH));
X2 = zeros(size(xH));
idx = xH > 0;
X1(idx) = xH(idx);
X2(idx) = xH(idx);
COUNTcol = sum(X1 > 0, 1);

2 件のコメント

mezali zineb
mezali zineb 2022 年 12 月 28 日
移動済み: Voss 2022 年 12 月 28 日
Thank you so much.
Voss
Voss 2022 年 12 月 28 日
編集済み: Voss 2022 年 12 月 29 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 12 月 27 日

編集済み:

2022 年 12 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by