You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中筛选坐标对应点的最小z值问题求助

解决匹配坐标点中保留最小z值的问题

要实现为每个coord点在coord1的匹配点(坐标差在delta范围内)里只保留z值最小的记录,我们可以通过跟踪每个点的最优匹配来实现。下面是具体的代码和思路:

步骤1:初始化数据(你的原始数据)

import numpy as np

# 第一组坐标
A = np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012])
B = np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587])
coord = np.array([A, B])

# 第二组坐标及对应的z值
C = np.array([160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421 ])
D = np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
z = np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
coord1 = np.array([C,D])

delta = 0.09

步骤2:跟踪每个coord点的最小z匹配记录

我们用一个字典来存储每个coord点(按索引i)对应的最小z值信息,遍历所有可能的匹配对并更新最优记录:

# 初始化字典,键为coord的索引i,值为该点的最优匹配信息
min_z_records = {}

for i in range(coord.shape[1]):
    current_coord_point = coord[:, i]
    # 遍历coord1的所有点
    for j in range(coord1.shape[1]):
        current_coord1_point = coord1[:, j]
        # 检查坐标是否在delta允许的范围内
        if np.all(current_coord_point >= current_coord1_point - delta) and np.all(current_coord_point <= current_coord1_point + delta):
            current_z_value = z[j]
            # 更新最优记录:如果i未被记录,或者当前z值更小
            if i not in min_z_records or current_z_value < min_z_records[i]['z_value']:
                min_z_records[i] = {
                    'coord_point': current_coord_point,
                    'coord1_point': current_coord1_point,
                    'z_value': current_z_value,
                    'coord1_index': j
                }

# 输出最终结果
for idx, record in min_z_records.items():
    print(f"{record['coord_point']} {idx} {record['coord1_point']} {record['z_value']} {record['coord1_index']}")

代码说明

  • 字典跟踪最优记录:min_z_records字典确保每个coord点(索引i)只会保留当前找到的z值最小的匹配记录。
  • 匹配判断逻辑:和你原来的逻辑一致,检查两个坐标的x、y值是否都在delta的范围内。
  • 结果输出:最后遍历字典,每个i只会输出一条z值最小的匹配记录。

预期输出

运行后,你会得到每个coord点对应的最小z值匹配记录,比如:

[160.592625  53.090028] 0 [160.514     53.068106] 0.0452326 0
[161.61683  54.829213] 1 [161.61683  54.829213] 0.127296 5
[161.672708  54.573222] 2 [161.67894   54.552493] 0.0452326 1

(其他无匹配的点不会出现在结果中,若需要保留所有i可以初始化时设置默认值)

内容的提问来源于stack exchange,提问作者user23299

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:41:12