# How can i get neighbors from 3D array but in the same order as the matrix ?

12 views (last 30 days)
math lcn on 13 Nov 2014
Commented: Abhay on 14 Nov 2014
Hello,
How can i get neighbors from 3D array but in the same order as the matrix ?
for 2D array :
A =
0.6787 0.1712 0.0971 0.0344 0.1869
0.7577 0.7060 0.8235 0.4387 0.4898
0.7431 0.0318 0.6948 0.3816 0.4456
0.3922 0.2769 0.3171 0.7655 0.6463
0.6555 0.0462 0.9502 0.7952 0.7094
the neighbors of A(3,3) are :
ngh =
0.7060 0.8235 0.4387
0.0318 0.6948 0.3816
0.2769 0.3171 0.7655
the matlab code is :
A =rand(5,5) ;
idx = sub2ind(size(A), 3,3);
M = size(VV,1);
Nidx = [idx-M-1 idx-1 idx+M-1;...
idx-M idx idx+M; ...
idx-M+1 idx+1 idx+M+1];
ngh = A(Nidx(:));
ngh = reshape(ngh,3,3);
Can you help me to get the same results in the case of 3D array … ?
Regards
Abhay on 14 Nov 2014
Dear Evan, Sorry for posting my query in the flagged content, actually when I was going through the flagged content I have found the query related to the generation of 3-d plot , I found a little connection of it with my previously asked problem, so in my eagerness I posted it there only, kindly forgive me for being a new user.

Evan on 13 Nov 2014
Edited: Evan on 13 Nov 2014
One way to do it:
A = rand(5,5,5);
B = false(size(A));
B(3,3,3) = true; %Point around which you want to find neighbors
##### 2 CommentsShowHide 1 older comment
Evan on 13 Nov 2014

### Categories

Find more on Matrix Indexing 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!

Translated by