How do I Vectorize the for loop containing if else ?

13 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 27 Dic 2022
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 Commenti
mezali zineb
mezali zineb il 28 Dic 2022
Spostato: Voss il 28 Dic 2022
Thank you so much.
Voss
Voss il 28 Dic 2022
Modificato: Voss il 29 Dic 2022
You're welcome!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by