Effacer les filtres
Effacer les filtres

vectorization of for loop

1 vue (au cours des 30 derniers jours)
Rizwan Khan
Rizwan Khan le 20 Juil 2022
Commenté : Rizwan Khan le 23 Juil 2022
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
for a = 1:h % for loops to reassign labels
for b = 1:w
for c = 1:p
if (o_lmat(a,b,c) ~= 0)
o_lmat(a,b,c) = o_lmat(a,b,c)+n_prev;
end
end
end
end
end
I am trying to vectorize this code as:
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
% o_lmat = zeros(h,w,p);
if (o_lmat(:,:,:) ~= 0)
o_lmat(:,:,:) = o_lmat(:,:,:)+n_prev;
end
end
It is not working correctly. Pllease find the mistake I am making.

Réponse acceptée

Chunru
Chunru le 20 Juil 2022
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
% for a = 1:h % for loops to reassign labels
% for b = 1:w
% for c = 1:p
% if (o_lmat(a,b,c) ~= 0)
% o_lmat(a,b,c) = o_lmat(a,b,c)+n_prev;
% end
% end
% end
% end
idx = o_lmat(:) ~=0;
o_lmat(idx) = o_lmat(idx) + n_prev;
end
  1 commentaire
Rizwan Khan
Rizwan Khan le 23 Juil 2022
thanks a lot

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by