在 MATLAB 中计算向量元素之间的最大距离
假设我们有一个向量,就像
x = -1:0.05:1;
ids = randperm(length(x));
x = x(ids(1:20));
我想以某种惯用的方式计算 x
元素之间的最大距离。迭代 x
元素的所有可能组合是很容易的,但我觉得可能有一种方法可以用 MATLAB 的内置函数以某种疯狂但惯用的方式做到这一点。
Let's assume that we have a vector like
x = -1:0.05:1;
ids = randperm(length(x));
x = x(ids(1:20));
I would like to calculate the maximum distance between the elements of x
in some idiomatic way. It would be easy to just iterate over all possible combinations of x
's elements but I feel like there could be a way to do it with MATLAB's built-in functions in some crazy but idiomatic way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
又怎样呢
?
What about
?
您的意思是向量中最大和最小元素之间的差异吗?如果你这样做,那么这样的事情就会起作用:
如果你不这样做,那么我误解了这个问题。
Do you mean the difference between the largest and smallest elements in your vector ? If you do, then something like this will work:
If you don't, then I've misunderstood the question.
这是一种点间距离计算,尽管很简单,因为您在一维中工作。实际上,在一维中落在最大距离处的点始终是两个可能点之一。因此,您所需要做的就是从列表中获取最小值和最大值,然后看看哪个距离问题点更远。因此,假设 x 中的数字是实数,这将起作用:
作为替代方案,前段时间我在文件交换上放置了一个通用的点间距离计算工具 (IPDM)。它足够聪明,可以处理特殊情况的简单问题,例如一维最远点问题。这个调用会为你做这件事:
当然,它不会像我上面写的简单代码那么有效,因为它是一个完全通用的工具。
This is an interpoint distance computation, although a simple one, since you are working in one dimension. Really that point which falls at a maximum distance in one dimension is always one of two possible points. So all you need do is grab the minimum value and the maximum value from the list, and see which is farther away from the point in question. So assuming that the numbers in x are real numbers, this will work:
As an alternative, some time ago I put a general interpoint distance computation tool up on the file exchange (IPDM). It is smart enough to special case simple problems like the 1-d farthest point problem. This call would do it for you:
Of course, it will not be as efficient as the simple code I wrote above, since it is a fully general tool.
呃...我很想拥有一个 MATLAB,而且现在还很早,但是像这样的东西怎么样:
我不知道这是否是您正在寻找的东西。
Uhh... would love to have a MATLAB at my hands and its still early in the morning, but what about something like:
I don't know if this is what You are looking for.