Buffer around a value in a matrix.

조회 수: 3 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by