How do I Vectorize the for loop containing if else ?

조회 수: 13 (최근 30일)
mezali zineb
mezali zineb 2022년 12월 27일
편집: Voss 2022년 12월 29일
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일
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개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by