I want to find whether the selected elements of a matrix are adjacent or not?
Show older comments
For e,g
A=[0 1 1 0;
0 1 0 0]
i have selected A(1,2), A(1,3), A(2,2) for use , i want to know that which pair is adjacent to other ?
3 Comments
Image Analyst
on 5 Apr 2015
If you have
A=[0 1 1 1;
0 1 0 0]
Do you consider the top right 1 adjacent to the bottom left 1? They are connected but not immediate (1 pixel away) neighbors.
Amir
on 6 Apr 2015
Image Analyst
on 6 Apr 2015
That's not what I asked. I want to know if you consider A(2,2) "adjacent" to A(1,3) because there is a path of 1's connecting them. If so, you can simply use bwlabel.
Accepted Answer
More Answers (1)
[i,j]=find(A);
AdjacencyMatrix = xor( abs(bsxfun(@minus,i,i.'))==1 , abs(bsxfun(@minus,j,j.'))==1 )
Don't know if you want to allow diagonal adjacency. The above assumes not.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!