Finding neighbours in a matrix

68 ビュー (過去 30 日間)
AelinAG
AelinAG 2018 年 9 月 11 日
コメント済み: Ethan 2022 年 11 月 20 日
Hi,
If I have an m x n matrix, how can I print a list of all the possible neighbours(horizontally, vertically and diagonally)? For example matrix(1, 1) should only give 3 values, while an element somewhere in the centre of the matrix should give 8 values. I know there are questions on this website resembling mine, but since I'm really new to matlab, I wondered if there are any easier solutions(I don't mind if they're longer than they'd need to be) Thank you very much!
  1 件のコメント
Steven Lord
Steven Lord 2018 年 9 月 11 日
I'm guessing you're not trying to find the neighbors simply to have a list of the neighbors. If you explain how you plan to use this information, what you want to obtain at the end, we may be able to offer more targeted suggestions for how to achieve your end goal.
For instance, since you said you're new to MATLAB, one reason you might want to do this is you've been given a homework assignment to program Conway's Game of Life. If so, after you've written up your version you might be interested in reading the chapter on the Game of Life in Cleve Moler's Experiments with MATLAB.

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2018 年 9 月 11 日
You can use blockproc(). It can do that. See attached test.m demo. If you don't want to have it "jump" by 3 pixels every time, but want it to move over by every time, you have to overlap the windows like I do in the other attached demo. See if you can adapt it, if you need that (being a smart engineer, I'm sure you can).
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 11 月 25 日
Naomi Pudney comments
I downloaded these and now MATLAB is giving me a huge error message and won't work properly.
Image Analyst
Image Analyst 2020 年 11 月 26 日
編集済み: Image Analyst 2020 年 11 月 26 日
I just downloaded test.m and it ran fine:
Same for blockproc_overlapping.
What error did you get? You forgot to say. Also, attach your m-file.

サインインしてコメントする。


KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 11 日
編集済み: Walter Roberson 2021 年 9 月 30 日
You can do it one by one, I don't think there is any direct command for that.
Suppose the present matrix position is i,j, matrix represent the given matrix.
%Here i,j represents the rows and colm value of the matrix
el_1=matrix(i,j) % Give the value of present element having position i,j
el_2=matrix(i,j-1) % Left element
el_3=matrix(i,j+1) % Right element
el_4=matrix(i-1,j) % for upper element
el_5=matrix(i+1,j)%Upper element
el_6=matrix(i-1,j-1) % West-North Diagonal Element....
.........
You can do for the rest from the following figure
Maybe indexing helps to reduce the lines, hope some expert will show the path in that direction.
  7 件のコメント
Image Analyst
Image Analyst 2022 年 11 月 19 日
@Ethan you assign them, like
i = 2;
j = 4;
value = matrix(i, j)
To learn other fundamental concepts, invest 2 hours of your time here:
Ethan
Ethan 2022 年 11 月 20 日
@Image Analyst thankyou, I'll check that out

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2018 年 9 月 11 日
Let a - your double array.
m = size(a)+2;
b = nan(m);
b(2:end-1,2:end-1) = a;
iii = reshape(1:numel(b),m);
ii = reshape(iii(~isnan(b)),1,1,[]);
out = reshape(b(ones(3).*ii + (-m(1):m(1):m(1)) + (-1:1)'),9,[]);
  2 件のコメント
Image Analyst
Image Analyst 2018 年 9 月 11 日
So if
a =
11 81 21 26 63 52 45
28 44 95 15 50 51 10
30 56 17 66 13 72 45
72 6 29 60 86 67 37
32 44 90 30 44 2 45
26 34 39 35 13 73 35
Then the first output should be [81,44,28] (the values of the 3 neighbors) and the second should be (if you move over one) [21,95,44,28,11] (the values of the 5 neighbors). Your code gives
out =
Columns 1 through 20
NaN NaN NaN NaN NaN NaN NaN 11 28 30 72 32 NaN 81 44 56 6 44 NaN 21
NaN NaN NaN NaN NaN NaN 11 28 30 72 32 26 81 44 56 6 44 34 21 95
NaN NaN NaN NaN NaN NaN 28 30 72 32 26 NaN 44 56 6 44 34 NaN 95 17
NaN 11 28 30 72 32 NaN 81 44 56 6 44 NaN 21 95 17 29 90 NaN 26
11 28 30 72 32 26 81 44 56 6 44 34 21 95 17 29 90 39 26 15
28 30 72 32 26 NaN 44 56 6 44 34 NaN 95 17 29 90 39 NaN 15 66
NaN 81 44 56 6 44 NaN 21 95 17 29 90 NaN 26 15 66 60 30 NaN 63
81 44 56 6 44 34 21 95 17 29 90 39 26 15 66 60 30 35 63 50
44 56 6 44 34 NaN 95 17 29 90 39 NaN 15 66 60 30 35 NaN 50 13
Columns 21 through 40
95 17 29 90 NaN 26 15 66 60 30 NaN 63 50 13 86 44 NaN 52 51 72
17 29 90 39 26 15 66 60 30 35 63 50 13 86 44 13 52 51 72 67
29 90 39 NaN 15 66 60 30 35 NaN 50 13 86 44 13 NaN 51 72 67 2
15 66 60 30 NaN 63 50 13 86 44 NaN 52 51 72 67 2 NaN 45 10 45
66 60 30 35 63 50 13 86 44 13 52 51 72 67 2 73 45 10 45 37
60 30 35 NaN 50 13 86 44 13 NaN 51 72 67 2 73 NaN 10 45 37 45
50 13 86 44 NaN 52 51 72 67 2 NaN 45 10 45 37 45 NaN NaN NaN NaN
13 86 44 13 52 51 72 67 2 73 45 10 45 37 45 35 NaN NaN NaN NaN
86 44 13 NaN 51 72 67 2 73 NaN 10 45 37 45 35 NaN NaN NaN NaN NaN
Columns 41 through 42
67 2
2 73
73 NaN
37 45
45 35
35 NaN
NaN NaN
NaN NaN
NaN NaN
Not sure I understand how to interpret that.
Andrei Bobrov
Andrei Bobrov 2018 年 9 月 12 日
編集済み: Walter Roberson 2021 年 9 月 30 日
interpretation of my result out:
We consider the matrix b-matrix a with boundary from nan elements.
The number of columns of the matrix out is equal to the number of elements of the matrix a.
Each column of the matrix out is a reshape of small region (3 by 3) of the matrix b.
b(1:3,1:3) -> out(:,1)
NaN NaN NaN NaN
NaN 11 81 NaN
NaN 28 44 -> NaN
NaN
11
28
NaN
81
44
b(2:4,1:3) -> out(:,2)
NaN 11 81 NaN
NaN 28 44 NaN
NaN 30 56 -> NaN
11
28
30
81
44
56
and so on.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by