简体   繁体   中英

np.linalg.norm on multiple rows in array

I'm trying to get the magnitude of each vector in an array that looks like this ;

[[0.1,1.0,2.0]
[0.5,2.0,1.0]
[0.4,3.0,0.5]]

I want to put these into a new numpy array like this ;

[[1.2]
[2.0]
[2.1]]

Currently i'm doing this with a loop;

for i in range(x)):
    d = vec2[i] - vec1[i]
    dist[i] = np.linalg.norm(d)

However i'd like to do this with pure numpy ( the above are numpy arrays) Is there a way i'm missing to do this on a single line? This does not work, as the values I get are not identical;

d = vec2 - vec1
dist = np.linalg.norm(d, axis=1)

使用axis = 0或axis = 1,这应该对每个数组执行计算,具体取决于您前进的方向

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