sample neighboring elements in a matrix

1 view (last 30 days)
Christopher
Christopher on 3 Oct 2014
Edited: Christopher on 3 Oct 2014
r = round(0.5+randi(8,size(10,10))*8); % I have the matrix of random integers
r1 = r(1:3:end,1:3:end); % I sample every element separated by 3 elements in 2D.
I now want to treat each random number as indicating a neighboring element as:
1 2 3
4 r 5
6 7 8
and return the corresponding neighboring element values in a new variable like:
r_neigh=r(code to find neighboring values);
but only for the elements in r1.
so if r=
6 7 5* 4* 1 3
4 1 6 8 7 4
2 4 6 3 8 1
3 2 2 4 6 5
3 3 5 1 4 5
2 7 1 5 7 7
Then element r(1,4)=4, would result in r_neigh(1,4)=5 (see asterisks^)
I suspect I can use a conv2 function or something, but I am not sure how because there are 8 possible neighbors. Thank you.
Edit: Also, I know that there will be problems with the boundaries of the matrix, I will cross that bridge when I come to it.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!