在 MATLAB 中计算向量元素之间的最大距离

发布于 2024-08-25 11:01:41 字数 229 浏览 11 评论 0原文

假设我们有一个向量,就像

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单气质女生网名 2024-09-01 11:01:41

又怎样呢

max_dist = max(x) - min(x)

What about

max_dist = max(x) - min(x)

?

红墙和绿瓦 2024-09-01 11:01:41

您的意思是向量中最大和最小元素之间的差异吗?如果你这样做,那么这样的事情就会起作用:

max(x) - min(x)

如果你不这样做,那么我误解了这个问题。

Do you mean the difference between the largest and smallest elements in your vector ? If you do, then something like this will work:

max(x) - min(x)

If you don't, then I've misunderstood the question.

怀中猫帐中妖 2024-09-01 11:01:41

这是一种点间距离计算,尽管很简单,因为您在一维中工作。实际上,在一维中落在最大距离处的点始终是两个可能点之一。因此,您所需要做的就是从列表中获取最小值和最大值,然后看看哪个距离问题点更远。因此,假设 x 中的数字是实数,这将起作用:

xmin = min(x);
xmax = max(x);
maxdistance = max(x - xmin,xmax - x);

作为替代方案,前段时间我在文件交换上放置了一个通用的点间距离计算工具 (IPDM)。它足够聪明,可以处理特殊情况的简单问题,例如一维最远点问题。这个调用会为你做这件事:

D = ipdm(x,'subset','farthest','result','struct');

当然,它不会像我上面写的简单代码那么有效,因为它是一个完全通用的工具。

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:

xmin = min(x);
xmax = max(x);
maxdistance = max(x - xmin,xmax - x);

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:

D = ipdm(x,'subset','farthest','result','struct');

Of course, it will not be as efficient as the simple code I wrote above, since it is a fully general tool.

述情 2024-09-01 11:01:41

呃...我很想拥有一个 MATLAB,而且现在还很早,但是像这样的东西怎么样:

max_dist = max(x(2:end) - x(1:end-1));

我不知道这是否是您正在寻找的东西。

Uhh... would love to have a MATLAB at my hands and its still early in the morning, but what about something like:

max_dist = max(x(2:end) - x(1:end-1));

I don't know if this is what You are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文