在2D数组中找到一个角值

发布于 2025-01-24 17:50:40 字数 257 浏览 4 评论 0原文

我在((0,1),(0,1))域中有一个随机分布的点。 我需要找到角值。 (沿0轴的最小值,沿1轴的最小值) (沿0轴的最大值,沿1轴的最小值) (沿0轴的最大值,最大沿1轴) (沿0轴的最小值,最大沿1轴) 问题在于同时搜索沿2个轴的最小值/最大值。 如何在numpy中实现这种模拟搜索?

import numpy as np    
np.random.seed(42)
points = np.random.random((100,2))   

I have a randomly distributed points inside the ((0,1), (0,1)) domain.
I need to find the corner values.
(min along 0 axis, min along 1 axis)
(max along 0 axis, min along 1 axis)
(max along 0 axis, max along 1 axis)
(min along 0 axis, max along 1 axis)
The problem is in the simultanuosly searching the min/max values along the 2 axes.
How to realize this simulataneous search in numpy?

import numpy as np    
np.random.seed(42)
points = np.random.random((100,2))   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

2025-01-31 17:50:40

您可以使用欧几里得距离找到最接近的点。从角落到那个点,找到向量的长度是eqvalent。对于(0,0)这很琐碎:

points[np.argmin(np.sum(np.square(points), axis=1)), :]

对于其他角落,您需要先找到它们之间的向量,例如(0,1

points[np.argmin(np.sum(np.square(points - [0, 1]), axis=1)), :]

)首先计算向量点 - [0,1],然后在该向量中平方两个值。然后,它使用np.Argmin在每一行上汇总,找到最小的值的参数或索引,并使用它来重新索引原始点数组来提取点坐标。

nb真正的欧几里得距离也将占据正方形之和的平方根,但是由于我们只是在寻找最小的,因此我们不需要采取这一步骤。

You could use the Euclidean distance to find the closest point. Which is eqivalent to finding the length of the vector from the corner to that point. For (0,0) it's trivial:

points[np.argmin(np.sum(np.square(points), axis=1)), :]

For the other corners you need to find the vector between them first, e.g. for (0,1):

points[np.argmin(np.sum(np.square(points - [0, 1]), axis=1)), :]

What this line does is first compute the vectors points - [0, 1] and then squares both values in that vector. It then sums across each row, finds the argument or index, of the smallest value using np.argmin and uses this to re-index the original points array to extract the point co-ords.

N.B. The true Euclidean distance would also take the square root of the sum of the squares, but since we're just looking for the smallest, we don't need to take that step.

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