MATLAB: How to find 26 neighbors of point with coordinates of (i,j,k)

digital image processingimage analysisimage segmentationmatrix arraymatrix manipulation

I have a stack of images in a 3D matrix.
How can I find 26 neighbors of a specific voxel with coordinates of (i,j,k) in Matlab?
Your help is appreciated.

Best Answer

To get a 27 element cube around the voxel, do this:
neighbors = matrix3d(i-1:i+1, j-1:j+1, k-1:k+1);
Then turn it into a column vector like this:
neighbors = neighbors(:);
Then delete the i,j,k element to get the final 26 neighbors, by deleting the middle element of the column vector:
neighbors[14] = []; % Now we're done! We have a list of 26 voxel values.