Borrar filtros
Borrar filtros

Replacing the numbers in a matrix

1 visualización (últimos 30 días)
mcl1993
mcl1993 el 11 de Abr. de 2017
Comentada: mcl1993 el 11 de Abr. de 2017
I have a matrix of numbers (22 x 22). Some of the numbers in the cells are zero. If the cell contains a zero, I want to replace in the numbers in the cells directly above, below, to the right and left also with a zero.

Respuesta aceptada

Jan
Jan el 11 de Abr. de 2017
Editada: Jan el 11 de Abr. de 2017
Do you mean all 8 surrounding elements?
x = randi([0,5], 22, 22); % Example data
Z = conv2(double(x == 0), ones(3, 3), 'same');
x(Z ~= 0) = 0;
If you mean the 4 neighbors without the diagonals:
Z = conv2(double(x == 0), [0,1,0; 1,0,1; 0,1,0], 'same');
x(Z ~= 0) = 0;
  1 comentario
mcl1993
mcl1993 el 11 de Abr. de 2017
I meant the 4 neighbours without the diagonals, thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 11 de Abr. de 2017
Editada: Andrei Bobrov el 11 de Abr. de 2017
Let A - your array [22 x 22]
A(bwdist(A==0) <= 1) = 0

Categorías

Más información sobre Operating on Diagonal Matrices 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