在Matlab中简化meshgrid的使用 [英] Simplifying the use of meshgrid in Matlab

查看:201
本文介绍了在Matlab中简化meshgrid的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

通过进行扩展这个问题。我有一个点集合(在三轴系统中,所以有(x,y,z)坐标),并且我想计算每个点之间的距离。

为此,我首先必须制作一个包含所有可能的点组合的矩阵(最好不要重复,以节省处理时间),然后计算所有距离。



问题



现在我正在尝试使用 meshgrid 复杂。它变得越来越复杂,因为(x,y,z)坐标位于矩阵中,格式为: pointCoordinates [x,y,z,pointnumber]



我不知道如何告诉 meshgrid 来结合point-1和point-2等,而不是将所有单独的x坐标与所有单独的y坐标等(这些组合太多了,其中大部分都是无用的)。

问题



如何让meshgrid保持坐标的冗余组合?或者有更简单的方法来做到这一点?






我想我可以重新格式化 pointCoordinates 矩阵转换为一个简单的字符串数组 points (使用与坐标相同数量的条目)。入口一(1,3,5),入口二(2,4,2)等,从而将坐标保持在一起,并限制可能的组合数量。但是这似乎是多余的。 解决方案

确实有一种更简单的方法。如果您只想计算非冗余点,则可以使用 pdist 。请注意,您可以选择与欧几里德不同的距离度量。

 距离= pdist(pointCoordinates(:,1:3)); 

来自帮助:

$ b (2,1),(3,1),...,(m,1),(3,2),...(m,2)的顺序排列输出D, ),.....(m,m-1)),即按列顺序的全m乘m距离矩阵的左下三角形。为了得到第i个和第j个观察值(i Background

Expanding upon this question. I have a collection of points (in a three axes system, so with (x,y,z) coordinates), and i want to calculate the distance between each point.

For that i first have to make a matrix with all possible combinations of points (preferably without duplicates, to save on processing time), to then calculate all the distances.

Problem

Now I am trying to use meshgrid for this, but it's getting pretty complicated. It's getting complicated because the (x,y,z) coordinates are in a matrix that is formatted as: pointCoordinates[x,y,z,pointnumber]

And i don't know how to tell meshgrid to just combine point-1 and point-2 etc., without also combining all the separate x-coordinates with all the separate y-coordinates, etc. (wich are far too many combinations, most of which are useless).

Question

How do i keep meshgrid from making redundant combinations of coordinates? Or is there a simpler way to do this?


I guess i could reformat the pointCoordinates matrix to a simple string array points (with as many entries as there are coordinates). Where entry one is (1,3,5), entry two (2,4,2), etc. Thereby keeping the coordinates together, and limiting the number of possible combinations. But that seems redundant.

解决方案

There is indeed a simpler way. If you only want to calculate non-redundant points, you can use pdist. Note that you can choose a different distance metric than Euclidean.

distances = pdist(pointCoordinates(:,1:3));

From the help:

The output D is arranged in the order of ((2,1),(3,1),..., (m,1),(3,2),...(m,2),.....(m,m–1)), i.e. the lower left triangle of the full m-by-m distance matrix in column order. To get the distance between the ith and jth observations (i < j), either use the formula D((i–1)*(m–i/2)+j–i), or use the helper function Z = squareform(D), which returns an m-by-m square symmetric matrix, with the (i,j) entry equal to distance between observation i and observation j.

这篇关于在Matlab中简化meshgrid的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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