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

信号分析项目:如何高效简洁判断三维向量象限并赋值?

优化方案建议

方案1:二进制编码+字典映射

利用每个维度的正负状态对应二进制位,通过字典建立编码值与note的映射关系,代码简洁且易维护:

# 生成每个维度的符号标识:1表示>0,0表示<0
sgn = [1 if x > 0 else 0 for x in ondas]
# 计算二进制编码值(权重分别为4、2、1,对应三个维度)
code = sgn[0] * 4 + sgn[1] * 2 + sgn[2] * 1
# 建立编码到note的映射
note_mapping = {
    7: 1,
    6: 2,
    4: 3,
    0: 4,
    2: 5,
    1: 6,
    3: 7,
    5: 8
}
# 获取对应的note,默认0(处理任意维度为0的情况)
note = note_mapping.get(code, 0)

方案2:元组键直接映射

用向量的符号状态元组作为字典键,可读性更强,无需额外计算编码值:

# 生成符号状态元组
sgn_tuple = (1 if ondas[0]>0 else 0, 1 if ondas[1]>0 else 0, 1 if ondas[2]>0 else 0)
# 直接建立状态到note的映射
note_mapping = {
    (1,1,1):1, (1,1,0):2,
    (1,0,0):3, (0,0,0):4,
    (0,1,0):5, (0,0,1):6,
    (0,1,1):7, (1,0,1):8
}
# 获取结果,默认0
note = note_mapping.get(sgn_tuple, 0)

方案3:批量数据的numpy优化

如果需要处理大规模信号数据(批量三维向量),用numpy可大幅提升效率:

import numpy as np

# 假设ondas是shape为(N,3)的numpy数组
sgn = (ondas > 0).astype(np.int8)
# 计算每个向量的编码值
codes = sgn[:,0] * 4 + sgn[:,1] * 2 + sgn[:,2] * 1
# 定义映射数组(索引对应编码值,值对应note)
mapping_arr = np.array([4,6,5,7,3,8,2,1])
# 生成note数组,编码值不在0-7时设为0
note = np.where((codes >=0) & (codes <=7), mapping_arr[codes], 0)

说明

  • 所有方案均保留原逻辑:任意维度为0时note=0
  • 方案1、2适合单向量处理,代码简洁易扩展;方案3针对批量场景,效率远超循环判断
  • 若后续扩展到n维向量,只需调整编码权重或映射键的结构即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 18:01:19