필터 지우기
필터 지우기

Determine adjacent points in a logical matrix

조회 수: 2 (최근 30일)
dormant
dormant 2023년 9월 4일
이동: Stephen23 2023년 9월 5일
I have a 2-D matrix of logical values, eg
000000000
010000000
000000000
000000000
000000000
000001000
000000000
000000000
100000000
How can I create a similar sized matrix with True in all the locations adjacent to the Trues in the first matrix, eg
111000000
101000000
111000000
000000000
000011100
000010100
000011100
110000000
010000000
If a location is assigned True from two or more adjacent locations, then it should be True.
  댓글 수: 1
dormant
dormant 2023년 9월 4일
이동: Stephen23 2023년 9월 5일
Thanks everyone. I knew it would be trivial, but I don't have much experience with manipulating 2D arrays.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2023년 9월 4일
편집: John D'Errico 2023년 9월 4일
Trivial. Learn to think in terms of MATLAB operations. I've added another 1 in there, just to make it clear what the problem is.
A = [0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0];
For example, what does this do? Does it get you close to what you want?
B = conv2(A,ones(3),'same')
B = 9×9
1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 2 1 0 0 0 0 0 1 2 2 1 0 0 0 0 0 1 2 2 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
Close, but we can fix that.
B = conv2(A,[1 1 1;1 0 1;1 1 1],'same')
B = 9×9
1 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 2 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 2 2 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
Next, we need to be careful, as two elements near each other can create something bigger than 1 due to the convolution. This next will correct that.
B = conv2(A,[1 1 1;1 0 1;1 1 1],'same') ~= 0
B = 9×9 logical array
1 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
The ones are adjacent to each other in the original array, and that meant that it filled in the ones in the result. We can zap them out too.
B = (conv2(A,[1 1 1;1 0 1;1 1 1],'same') ~= 0) & (~A)
B = 9×9 logical array
1 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
  댓글 수: 2
dormant
dormant 2023년 9월 4일
This is perfect. Many thanks.
John D'Errico
John D'Errico 2023년 9월 4일
Thank you. I hope the explanations made sense. The important point is to think of conv and conv2 when you have problems like this.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

DGM
DGM 2023년 9월 4일
This can also be done succinctly with image processing tools:
% a logical array
A = [0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0];
A = logical(A);
% output is a logical array
B = imdilate(A,ones(3)) & ~A
B = 9×9 logical array
1 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by