Avoiding sub2ind and ind2sub
问题 I need to access several indices around a certain point in 3D. For example, for point ( x1 , y1 , z1 ) I need to get all the indices of its 3x3x3 neighborhood such that ( x1 , y1 , z1 ) is centered. For neighborhood of size 3, I do it with [x,y,z] = meshgrid(-1:1,-1:1,-1:1); x_neighbors = bsxfun(@plus,x,x1); y_neighbors = bsxfun(@plus,y,y1); z_neighbors = bsxfun(@plus,z,z1); Here, I center x1 , y1 , z1 to (0,0,0) by adding the distances from ( x1 , y1 , z1 ) to any point in the 3x3x3 box.