Finding neighbours in a matrix

69 visualizaciones (últimos 30 días)
AelinAG
AelinAG el 11 de Sept. de 2018
Comentada: Ethan el 20 de Nov. de 2022
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 comentario
Steven Lord
Steven Lord el 11 de Sept. de 2018
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.

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 11 de Sept. de 2018
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 comentarios
Walter Roberson
Walter Roberson el 25 de Nov. de 2020
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 el 26 de Nov. de 2020
Editada: Image Analyst el 26 de Nov. de 2020
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.

Iniciar sesión para comentar.


KALYAN ACHARJYA
KALYAN ACHARJYA el 11 de Sept. de 2018
Editada: Walter Roberson el 30 de Sept. de 2021
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 comentarios
Image Analyst
Image Analyst el 19 de Nov. de 2022
@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 el 20 de Nov. de 2022
@Image Analyst thankyou, I'll check that out

Iniciar sesión para comentar.


Andrei Bobrov
Andrei Bobrov el 11 de Sept. de 2018
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 comentarios
Image Analyst
Image Analyst el 11 de Sept. de 2018
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 el 12 de Sept. de 2018
Editada: Walter Roberson el 30 de Sept. de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by