How to convert for loop statement to matrix operation?

Hi, I am very new to using Matlab. Due to the processing issue, I would like to convert for-loop statement to matrix calculation to accelerate processing time.
For example,
[u v] = size(input_img);
for i=1:u
for j=1:v
if (input_img(i,j) == 0)
temp(i,j)=0;
end
end
end
Thank you.

 Respuesta aceptada

James Tursa
James Tursa el 27 de Ag. de 2015
Editada: James Tursa el 27 de Ag. de 2015
Assuming the matrix sizes are compatible, using logical indexing:
temp( input_img == 0 ) = 0;

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 27 de Ag. de 2015

Comentada:

el 27 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by