简体   繁体   English

轴超出范围

[英]axis out of bounds with array

I have written a code that looks like this. 我写了这样的代码。

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)

The text files look something like this. 文本文件看起来像这样。

A.txt A.txt

23.172,-20.751,31.982
23.049,-20.789,32.164
22.914,-20.952,32.14

B.txt B.txt

21.879,-17.819,34.467
21.727,-17.975,34.311
21.804,-18.267,34.462

C.txt C.txt

20.273,-20.379,34.271
20.144,-20.614,34.36
20.065,-20.765,34.408

D.txt D.txt

21.936,-19.639,33.555
21.771,-19.7,33.506
21.581,-19.955,33.543

However, I have been getting an error message that says 但是,我收到一条错误消息,内容为

File "test.py", line 25, in distvec(p0,p1,p3,p,d) File "test.py", line 17, in distvec n = np.cross(u, v) File "/Users/Sam/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 1709, in cross axisa = normalize_axis_index(axisa, a.ndim, msg_prefix='axisa') numpy.core._internal.AxisError: axisa: axis -1 is out of bounds for array of dimension 0 文件“ 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行,横轴a = normalize_axis_index(axisa,a.ndim,msg_prefix ='axisa')numpy.core._internal。 AxisError:axisa:轴-1超出维度0数组的范围

Does anyone know what went wrong? 有人知道出了什么问题吗? Thanks so much! 非常感谢!

np.cross(u, v) returns the cross product of two vectors. np.cross(u, v)返回两个向量的叉积。 In your case, u and v are not vectors but scalars which have dimension 0. This is because you are having u = p0 - p3 where p0 and p3 are j th element of your array ( p0 = p0[j] ). 在您的情况下, uv不是向量,而是维数为0的标量。这是因为您具有u = p0 - p3 ,其中p0p3是数组的第j个元素( p0 = p0[j] )。

To verify, you can print u and v and check if they are scalars or a 1-d array (vectors). 要进行验证,可以打印uv并检查它们是标量还是一维数组(向量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遇到 AxisError:轴 1 超出维度 0 数组的范围 - Encountering AxisError: axis 1 is out of bounds for array of dimension 0 分类器:轴 1 超出维度 1 数组的范围 - classifier : axis 1 is out of bounds for array of dimension 1 AxisError:轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 numpy.AxisError:轴 1 超出维度 1 数组的范围 - numpy 数组 - numpy.AxisError: axis 1 is out of bounds for array of dimension 1 - numpy array numpy数组遍历错误:IndexError:索引1超出了大小为1的轴0的范围 - numpy array traversal Error: IndexError: index 1 is out of bounds for axis 0 with size 1 我无法解决“轴-1超出维度0数组的范围”的问题 - I can't solve issue “axis -1 is out of bounds for array of dimension 0” Numpy 连接给出错误:轴 1 超出维度 1 数组的范围 - Numpy concatenate giving error: axis 1 is out of bounds for array of dimension 1 如何解决 AxisError:轴 1 超出维度 0 数组的范围 - How to solve AxisError: axis 1 is out of bounds for array of dimension 0 numpy.AxisError:来源:轴 2 超出了维度 2 数组的范围 - numpy.AxisError: source: axis 2 is out of bounds for array of dimension 2 NumPy数组IndexError:索引99超出了大小为1的轴0的范围 - NumPy array IndexError: index 99 is out of bounds for axis 0 with size 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM