简体   繁体   中英

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

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

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

Does anyone know what went wrong? Thanks so much!

np.cross(u, v) returns the cross product of two vectors. 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] ).

To verify, you can print u and v and check if they are scalars or a 1-d array (vectors).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM