vectorization of for loop

1 visualización (últimos 30 días)
Rizwan Khan
Rizwan Khan el 20 de Jul. de 2022
Comentada: Rizwan Khan el 23 de Jul. de 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.

Respuesta aceptada

Chunru
Chunru el 20 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by