在matlab中计算3D点对的欧几里德距离 [英] Calculating Euclidean distance of pairs of 3D points in matlab

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

问题描述

我有一个包含 N 个 3D 点的 Nx3 数组

I have an Nx3 array that contains N 3D points

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

我想在一个 NxN 数组中计算欧氏距离,该数组测量每对 3D 点之间的欧氏距离.(i,j) 在结果数组中返回 (ai,bi,ci)(aj,bj,cj) 之间的距离).是否可以在没有循环的情况下在 matlab 中编写代码?

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 函数提供合适的维度来克服这一挑战.默认情况下,当我们调用 bsxfun 函数时,X 和 ReshapeX 应该具有相同的尺寸.但是,如果矩阵的大小不相等,并且其中一个具有单一维度(等于 1),则该矩阵实际上会沿着该维度复制以匹配另一个矩阵.因此,它返回 N*3*N 矩阵,该矩阵提供每个 3D 点与其他点的减法.

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中计算3D点对的欧几里德距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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