How to find the list of neighbors?
조회 수: 7 (최근 30일)
이전 댓글 표시
If I have an 150 x 150 matrix, how can I print a list of all the possible neighbours(horizontally, vertically and diagonally)?
댓글 수: 5
Walter Roberson
2023년 11월 16일
Someone using your account previously posted,
"I have a 150x150 matrix which represents grains of a of an engineering material. I wanted help with a code on how to find the neighbors of these grains and use it draw a node and vertex graph."
답변 (1개)
Walter Roberson
2023년 11월 16일
Let the array be height H and width W. Then for any given point with linear index L, the surrounding points are:
- (L-1-H) --> point diagonal up and left, provided that mod(L-1,H) ~= 0 and L > H (that is, not already on first row, and not in first column)
- (L-H) --> point "left", provided that L > H (that is, not in the first column)
- (L+1-H) --> point diagonal down and left, provided that mod(L,H) ~= 0 and L > H (that is, not already on the last row, and not in the first column)
- (L-1) --> point "above", provided that mod(L-1,H) ~= 0 (that is, not already in the first row)
- (L) --> point itself
- (L+1) --> point "below", provided that mod(L,H) ~= 0 (that is, not already on the last row)
- (L-1+H) --> point diagonal up and right, provided that mod(L-1,H)~=0 and L - 1 + H <= H*W (that is, not already on first row, and not already in the last column
- (L+H) --> point "right", provided that L+H<H*W (that is, not already in the last column)
- (L+1+H) --> point diagonally right and down, provided that mod(L,H) ~= 0 and L+H+1<=H*W (that is, not already on last row and not already in the last column)
(Really though the tests can be simplified a lot if you use ind2sub() and test the row and column components.)
I have to wonder what you are doing though, as a lot of the time it can just be easier to construct
idx = padarray(reshape(1:H*W, H, W), [1 1], 'both');
after which you can index
idx(row:row+2, col:col+2)
to get a 3 x 3 block of indices with 0's indicating places out of range.
댓글 수: 8
Image Analyst
2023년 11월 16일
편집: Image Analyst
2023년 11월 16일
Upload your matrix in a .mat file. Do you have a matrix of 0's and 1's? Do you want
- a list of all possible neighbors for each and every pixel regardless of its value,
- or just all the possible neighbors for the pixels with value 1,
- or just the neighbors of 1-valued pixels that are also 1-valued?
By you want a "print a list" do you mean use fprintf to write the row and column to the command window, and maybe the value of the pixel also?
By "find the neighbors" do you want just the location (if so, in x,y format or row,column format?) or do you want the value, or do you want both the value and location?
For all possible neighbors, you will have 8 possible neighbors except for the corners where you will have 3 possible neighbors and the edges where you will have 5 neighbors. Of course it's not ALL POSSIBLE neighbors, but just 1-valued neighbors then the number of neighbors could vary from 3 to 8 from location to location.
Walter Roberson
2023년 11월 16일
They start with an image, but through some unknown process they have created a label matrix (they indicate when I asked.)
They now want to know, for each grain, which other grains touch it in each of the 8 directions.
They need to create a graph in which the grains are nodes and touching is shown as an edge.
(We might recognize that because grains are irregular shaped, that identifying the grains in the 8 directions might not be sufficient to create a connection graph, but the 8-direction thing is what they asked for.)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!