Filter löschen
Filter löschen

Buffer around a value in a matrix.

2 Ansichten (letzte 30 Tage)
SChow
SChow am 8 Dez. 2022
Beantwortet: Jonas am 8 Dez. 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 Kommentare
Jonas
Jonas am 8 Dez. 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
SChow am 8 Dez. 2022
Bearbeitet: SChow am 8 Dez. 2022
Yes, exactly. Sorry for the glitch there.
Corrected now

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jonas
Jonas am 8 Dez. 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

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by