Buffer around a value in a matrix.
    6 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hi I am struggling with building a buffer of values around a specific value in a matrix. For example I have a matrix 
A=[ 0 0 0 0 1 0 0 1
    1 0 0 0 0 1 1 1
    0 0 0 0 1 1 1 1
    0 0 0 0 1 1 1 0
    1 0 0 0 1 1 0 0]
The intended matrix with a square buffer filled by 2 around all cells which are 1 (cells which are adjascent and diagonal). Thanks
B=[ 2 2 0 2 1 2 2 1
    1 2 0 2 2 1 1 1
    2 2 0 2 1 1 1 1
    2 2 0 2 1 1 1 2
    1 2 0 2 1 1 2 2]
2 Commenti
  Jonas
      
 il 8 Dic 2022
				i guess it should be 
B=[ 2 2 0 2 1 2 2 1
    1 2 0 2 2 1 1 1
    2 2 0 2 1 1 1 1
    2 2 0 2 1 1 1 2
    1 2 0 2 1 1 2 2]
?
Risposta accettata
  Jonas
      
 il 8 Dic 2022
        A=[ 0 0 0 0 1 0 0 1
    1 0 0 0 0 1 1 1
    0 0 0 0 1 1 1 1
    0 0 0 0 1 1 1 0
    1 0 0 0 1 1 0 0];
B=imfilter(A,ones(3))
B(B>0)=2
B(A==1)=1
isequal(B,[ 2 2 0 2 1 2 2 1
    1 2 0 2 2 1 1 1
    2 2 0 2 1 1 1 1
    2 2 0 2 1 1 1 2
    1 2 0 2 1 1 2 2])
0 Commenti
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

