Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用另一个数组求和-python_Python_Arrays_Numpy_Indexing_Sum - Fatal编程技术网

用另一个数组求和-python

用另一个数组求和-python,python,arrays,numpy,indexing,sum,Python,Arrays,Numpy,Indexing,Sum,我有两个对应的二维阵列,一个是速度,一个是强度。强度值与每个速度元素匹配 我已经创建了另一个一维阵列,它在均匀的箱子宽度中从最小速度到最大速度 如何将2d数组中的强度值相加,该值对应于一维数组中的速度箱 例如:如果I=5对应于速度=101km/s,那么这将添加到100-105 km/s的箱子中 以下是我的意见: rad = np.linspace(0, 3, 100) # polar coordinates phi = np.linspace(0, np.pi, 100) r, theta =

我有两个对应的二维阵列,一个是速度,一个是强度。强度值与每个速度元素匹配

我已经创建了另一个一维阵列,它在均匀的箱子宽度中从最小速度到最大速度

如何将2d数组中的强度值相加,该值对应于一维数组中的速度箱

例如:如果I=5对应于速度=101km/s,那么这将添加到100-105 km/s的箱子中

以下是我的意见:

rad = np.linspace(0, 3, 100) # polar coordinates
phi = np.linspace(0, np.pi, 100)

r, theta = np.meshgrid(rad, phi) # 2d arrays of r and theta coordinates

V0 = 225 # Velocity function w/ constants.
rpe = 0.149
alpha = 0.003

Vr = V0 * (1 - np.exp(-r / rpe)) * (1 + (alpha * np.abs(r) / rpe)) # returns 100x100 array of Velocities.

Vlos = Vr * np.cos(theta)# Line of sight velocity assuming the observer is in the plane of the polar disk.

a = (r**2) # intensity as a function of radius
b = (r**2 / 0.23)
I = (3.* np.exp(-1. * a)) - (1.8 * np.exp(-1. * b))
我希望首先创建从Vmin到Vmax的速度箱,然后对每个箱的强度求和

我所期望的输出将是与

V_bins = [0, 5, 10,... Vlos.max()]

I_sum = [1.4, 1.1, 1.8, ... 1.2]

plot(V_bins, I_sum)
编辑:我已经想出了一个临时解决方案,但也许有一个更优雅/有效的方法来实现它

两个阵列VLO和I都是100 x 100矩阵

Vlos = array([[ 0., 8.9, 17.44, ..., 238.5],...,
[-0., -8.9, -17.44, ..., -238.5]])

I = random.random((100, 100))



V = np.arange(Vlos.min(), Vlos.max()+5, 5)

bins = np.zeros(len(V))

for i in range(0, len(V)-1):
    for j in range(0, len(Vlos)): # horizontal coordinate in matrix
        for k in range(0, len(Vlos[0])): # vert coordinate

            if Vlos[j,k] >= V[i]and Vlos[j,k] < V[i+1]:
                bins[i] = bins[i] + I[j,k]
Vlos=array([0,8.9,17.44,…,238.5],。。。,
[-0., -8.9, -17.44, ..., -238.5]])
I=随机。随机((100100))
V=np.arange(Vlos.min(),Vlos.max()+5,5)
箱=np.零(len(V))
对于范围(0,len(V)-1)内的i:
对于范围(0,len(Vlos))中的j:#矩阵中的水平坐标
对于范围(0,len(Vlos[0]))内的k:#垂直坐标
如果Vlos[j,k]>=V[i]且Vlos[j,k]
结果如下图所示。 直方图中的整体形状是可以预期的,但是我不理解V=0时曲线中的尖峰。据我所知,数据中没有这一点,这让我质疑我的方法

任何进一步的帮助都将不胜感激

import numpy as np
bins = np.arange(100,120,5)
velocities = np.array([101, 111, 102, 112])
intensities = np.array([1,2,3,4])
h = np.histogram(velocities, bins, weights=intensities)
print h
输出:

(array([4, 0, 6]), array([100, 105, 110, 115]))

1.你试了什么?2.请举例说明你正在尝试做什么,否则很难理解。你正在寻找的是一个。有关更多信息,请参阅。请提供生成示例输入的代码,并尽可能准确地编写所需输出。我已经添加了一些关于我的代码和所需输出的解释,希望这有帮助。如果我知道如何使用相应的速度求和强度,我可以使用此方法。是否有一种方法(可能使用索引?)将强度数组的元素与速度数组相加。直方图就是这样做的。如果您还需要其他东西,请给我们一个具体的例子,包括输入和输出数字。加上方程式会使我们更加困难。