对计算的MATLAB中的三维点的欧氏距离 [英] Calculating Euclidean distance of pairs of 3D points in matlab

查看:2104
本文介绍了对计算的MATLAB中的三维点的欧氏距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含N个3D百分点NX3阵列

I have an Nx3 array that contains N 3D points

a1 b1 c1 
a2 b2 c2
.... 
aN bN cN 

欲计算在N×N个阵列,其测量每对三维点之间的欧几里得距离的欧几里德距离。 ( I Ĵ)的结果数组返回(AI,BI,CI之间的距离)(AJ,BJ,CJ)。是否有可能写在MATLAB一个code,无循环?

I want to calculate Euclidean distance in a NxN array that measures the Euclidean distance between each pair of 3D points. (i,j) in result array returns the distance between (ai,bi,ci) and (aj,bj,cj). Is it possible to write a code in matlab without loop ?

推荐答案

你的问题的挑战是使N * N矩阵,其结果应在此矩阵返回,而不使用循环。
我克服给予适当的维度Bsxfun功能这一挑战。默认情况下X和ReshapedX应该具有相同的尺寸,当我们调用函数bsxfun。但是,如果矩阵的大小不相等,并将它们中的一个具有一个单(等于1)维,基质几乎沿着该维度的其它基质匹配复制。因此,它返回N * 3 * N矩阵提供了从其他每个三维点的减法。

The challenge of your problem is to make a N*N matrix and the result should return in this matrix without using loops. I overcome this challenge by giving suitable dimension to Bsxfun function. By default X and ReshapedX should have the same dimensions when we call bsxfun function. But if the size of the matrixes are not equal and one of them has a singleton (equal to 1) dimension, the matrix is virtually replicated along that dimension to match the other matrix. Therefore, it returns N*3*N matrix which provides subtraction of each 3D point from the others.

ReshapedX = permute(X,[3,2,1]);
DiffX = bsxfun(@minus,X,ReshapedX);
DistX =sqrt(sum(DiffX.^2,2));
D = squeeze(DistX);

这篇关于对计算的MATLAB中的三维点的欧氏距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆