MATLAB: Distance between a point and elements of a matrix

efficient computation of distances

Dear;
I am looking for an efficient way to compute the distance between coordinates (x,y,z) and the elements of a 3D matrix. For expample for a M3D[1:nx,1:ny,1:nz] I would like to get ahother 3D matrix Distance3D where each element in the new matrix will be:
M3D(ix,iy,iz)=sqrt((ix-x)^2+(iy-y)^2+(iz-z)^2);
Where ix runs for 1 to nx, so on.
Thanks in advance;

Best Answer

This could be the solution
[ny,nx,nz]=size(M3D);
[xx,yy,zz] = meshgrid((1:nx)-x,(1:ny)-y,(1:nz)-z);
M3D_dist=sqrt(xx.^2 + yy.^2 + zz.^2);