Buffer around a value in a matrix.

3 visualizzazioni (ultimi 30 giorni)
SChow il 8 Dic 2022
Risposto: Jonas il 8 Dic 2022
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 CommentiMostra NessunoNascondi Nessuno
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]
?
SChow il 8 Dic 2022
Modificato: SChow il 8 Dic 2022
Yes, exactly. Sorry for the glitch there.
Corrected now

Accedi per commentare.

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 = 5×8
1 1 0 1 2 3 4 3 1 1 0 2 4 6 7 5 1 1 0 2 5 8 8 5 1 1 0 3 6 8 6 3 1 1 0 2 4 5 3 1
B(B>0)=2
B = 5×8
2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2
B(A==1)=1
B = 5×8
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
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])
ans = logical
1
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Logical 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!

Translated by