Buffer around a value in a matrix.

2 ビュー (過去 30 日間)
SChow
SChow 2022 年 12 月 8 日
回答済み: Jonas 2022 年 12 月 8 日
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 件のコメント
Jonas
Jonas 2022 年 12 月 8 日
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 2022 年 12 月 8 日
編集済み: SChow 2022 年 12 月 8 日
Yes, exactly. Sorry for the glitch there.
Corrected now

サインインしてコメントする。

採用された回答

Jonas
Jonas 2022 年 12 月 8 日
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 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by