我写了这样的代码。
import numpy as np
import math
p0 = np.loadtxt('A.txt', delimiter=',')
p1 = np.loadtxt('B.txt', delimiter=',')
p3 = np.loadtxt('C.txt', delimiter=',')
p = np.loadtxt('D.txt', delimiter=',')
d = 3
def distvec(p0,p1,p3,p,d):
vec=[]
for j in range(d):
p0 = p0[j]
p1 = p1[j]
p3 = p3[j]
p = p[j]
u = p0 - p3
v = p1 - p3
n = np.cross(u, v)
norm = math.sqrt(np.dot(n,n))**(-1)
n=n*norm
p_ = p - p0
dist_to_plane = np.dot(p_, n)
dist=math.sqrt(dist_to_plane**2)
vec=vec+[dist]
return vec
distvec(p0,p1,p3,p,d)
文本文件看起来像这样。
A.txt
23.172,-20.751,31.982
23.049,-20.789,32.164
22.914,-20.952,32.14
B.txt
21.879,-17.819,34.467
21.727,-17.975,34.311
21.804,-18.267,34.462
C.txt
20.273,-20.379,34.271
20.144,-20.614,34.36
20.065,-20.765,34.408
D.txt
21.936,-19.639,33.555
21.771,-19.7,33.506
21.581,-19.955,33.543
但是,我收到一条错误消息,提示
文件“ test.py”,第25行,在 distvec(p0,p1,p3,p,d) 文件“ test.py”,第17行,在distvec中 n = np.cross(u,v) 交叉的文件“ /Users/Sam/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py”,行1709 axisa = normalize_axis_index(axisa,a.ndim,msg_prefix ='axisa') numpy.core._internal.AxisError:axisa:轴-1超出维度0数组的范围
有人知道出了什么问题吗?非常感谢!
答案 0 :(得分:0)
np.cross(u, v)
返回两个向量的叉积。在您的情况下,u
和v
不是向量,而是维数为0的标量。这是因为您拥有u = p0 - p3
,其中p0
和p3
是{数组(j
的第{1}}个元素。
要进行验证,可以打印p0 = p0[j]
和u
并检查它们是标量还是一维数组(向量)。