[英]Dividing all elements in a multidimensional array by an array of numbers Numpy Python
我有一個 python 問題:
如果我有一個多維數組,可以說形狀 (3,4) 和一個具有 3 個值的一維數組,例如如下所示:
profile = np.array([[1,2,3,5],[3,6,2,5],[2,8,3,5]])
radius = np.array([4,8,3])
我正在尋找一種將多維數組的第一個/第二個元素中的所有值除以一維數組的相應第一個/第二個數字的方法。 所以作為一個 output 我基本上是這樣的:
profile_new = np.array[[1,2,3,5]/4], [[3,6,2,5]/8], [[2,8,3,5]/3]
我試過了:
for i,j in zip(radius_prof, r_500):
radius = i/j
但這會產生一個僅由 4 個元素組成的數組,但我又想要一個形狀為 (3,4) 的多維數組。
我希望我的問題是可以理解的,這是我在這里的第一篇文章,我找不到任何類似的問題。
提前致謝!
您需要將append
結果添加到以下列表for
:
import numpy as np
profile = np.array([[1,2,3,5],[3,6,2,5],[2,8,3,5]])
radius = np.array([4,8,3])
result = []
for i,j in zip(profile, radius):
result.append(i/j)
result
result = [x/y for x,y in zip(profile,radius) ]
result = np.divide(profile.T, radius).T
[array([0.25, 0.5 , 0.75, 1.25]),
array([0.375, 0.75 , 0.25 , 0.625]),
array([0.66666667, 2.66666667, 1. , 1.66666667])]
使用簡單的划分和廣播呢?
profile = np.array([[1,2,3,5],[3,6,2,5],[2,8,3,5]])
radius = np.array([4,8,3])
print(profile / radius[:, None])
# [[0.25 0.5 0.75 1.25 ]
# [0.375 0.75 0.25 0.625 ]
# [0.66666667 2.66666667 1. 1.66666667]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.