distance between a point and elements of a matrix

2 ビュー (過去 30 日間)
Luis Isaac
Luis Isaac 2017 年 4 月 12 日
回答済み: Luis Isaac 2017 年 4 月 21 日
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;

採用された回答

Luis Isaac
Luis Isaac 2017 年 4 月 21 日
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);

その他の回答 (2 件)

Fei Deng
Fei Deng 2017 年 4 月 17 日
Are you calculating the distance between coordinates (x,y,z) and the elements of a 3D matrix, OR the distance between coordinates (x,y,z) and the index of a 3D matrix? From your equation, it should be latter case, isn't it? In that case, I probably would just use 3 for-loops.

Luis Isaac
Luis Isaac 2017 年 4 月 21 日
Many Thanks
Yes it is the second; I am traing to calculate de distance from a point with coordinates (x,y,z) and the indexes of a 3D matrix. Of course, 3 for-loops is one of the solution the question, but I am looking for a vectoriced and quicker version of this problem.

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by