# How to use 2D logical mask on an image without for loop?

9 visualizaciones (últimos 30 días)
xiaojuezi el 28 de Abr. de 2020
Comentada: KSSV el 28 de Abr. de 2020
I have two image matrices A and B with size 512 x 512 x 3, and a logical mask of size 512 x 512. For each position with true value in the mask, I want to set A's value to B's. Currently I only know the for loop solution:
for y = 1 : height
for x = 1 : width
A(y,x,:) = B(y,x,:)
end
end
end
Is there a way to avoid the for loop?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

KSSV el 28 de Abr. de 2020
B = zeros(512,512,3) ;
for i = 1:3
T1 = B(:,:,1) ;
T2 = A(:,:,i) ;
T1(idx) = T2(idx) ;
B(:,:,i) = T1 ;
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
xiaojuezi el 28 de Abr. de 2020
Does this solution set B's value as A's instead of other way round as in the for loop?
KSSV el 28 de Abr. de 2020
It sets A's values to B. You can change it, if you want the other way.

Iniciar sesión para comentar.

### Categorías

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

### Community Treasure Hunt

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

Start Hunting!

Translated by