필터 지우기
필터 지우기

How to find neighbor matrix?

조회 수: 1 (최근 30일)
Tan Wen Kun
Tan Wen Kun 2015년 12월 3일
댓글: Walter Roberson 2015년 12월 7일
This is some code i write:
for i=1:h %h=height of the image
for j=1:w %w=width of image
if (i,j)==1 %1 is the border
if (i,j-1) ~=1 && if(i,j+1) ~=1
table(j-1,j+1) =1
table(j+1,j-1) =1
for j=1:w %w=width of image
for i=1:h %h=height of the image
if (j,1)==1 %1 is the border
if (j,i-1) ~=1 && if(j,i+1) ~=1
table(i-1,i+1) =1
table(i+1,i-1) =1
I hope loop horizontal and vertical to find the neighbor matrix dont care of diagonal situation.
As long as it read x then read 1 then read y then (x,y) and(y,x)=1
How to solve this?
A=
1 1 1 1 1 1 1 1 1 1
1 2 2 1 3 3 1 4 4 1
1 2 2 1 3 1 5 5 5 1
1 1 1 1 1 1 1 1 1 1
result=
2 3 4 5
2 1 1 0 0
3 1 1 1 1
4 0 1 1 1
5 0 1 1 1
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 12월 3일
table((i-1),(j+1)) attempts to index table(0,2) because i starts at 1.

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

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 3일
table = (A == 1);
s = max(size(table,1),size(table,2));
if s ~= size(table,1) || s ~= size(table,2)
table(s,s) = false; %make it square
end
table = table | table.'; %make it symmetric
  댓글 수: 2
Tan Wen Kun
Tan Wen Kun 2015년 12월 7일
편집: Tan Wen Kun 2015년 12월 7일
Your solution just extracted 1 from the labelimg, that not I want.
What I want is when is label= 1 2 1 3 1
I made table(2,3)=1 and table(3,2)=1
Attempted to access labelimg(214,0); index must be a positive integer or logical.
for i=1:max(labelimg(:))
for j=1:max(labelimg(:))
if labelimg(i,j)==1 %1 is the border
if labelimg(i,j-1) ~=1
if labelimg(i,j+1) ~=1
table(j-1,j+1) =1 ;
table(j+1,j-1) =1 ;
how to solve this when I using j-1 when loop first element got problem and also j+1 when loop last element?How to solve the index out of bound?
Walter Roberson
Walter Roberson 2015년 12월 7일
maxlab = max(labelimg(:));
for i = 1 : maxlab
for j = 2 : maxlab - 1
Now you can test labelimg(i,j-1) and labelimg(i,j+1)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by