由另一个数组索引的两个 numpy 数组的元素最小值

Element-wise minimum of two numpy arrays indexed by another array

我有三个形状数组:

A = a = np.random.exponential(1, [10, 1000000])  # of shape (10, 1000000)
B = a = np.random.exponential(1, [10, 1000000])  # of shape (10, 1000000)

我计算了另一个数组 IND[ ],如下所示。 IND[ ] 的每个元素是 A 的最大元素的索引(一列中每 10 个值的最大值),

IND = np.argmax(snr_sr, axis=0)  # of shape (1000000,)

我想计算另一个数组 C,它包含由 IND[ ] 的值指定的第 # 行中 A 和 B 的逐元素最小值。因此 C 数组的形状应该是 (1, 1000000)。我想避免 for 循环。我尝试了下面的方法,但是 C 的值不正确。

for j in range(0, A.shape[1]):    
        m  = ind[j]
        C  = minimum(A[m,:], B[m,:])  # return 1x1000000 array

Sorry, as the arrays are large, could not post it. You can take any arrays of the same shapes.

First Edit: Somebody provided me with the right answer, but he deleted it (don't know why?) Anyhow, I copied the answer before he deleted it. Please post it again so that I can mark it correct. (To him: Who took arrays of 1,100 for simplicity).

注意:我对numpy不精通,可能会有更高效的解决方案。

首先找到 A 和 B 行中值的最小值:

minAB = np.min(np.minimum(A, B), axis=1)

然后使用ind索引这个数组:

C = minAB[ind]

我缩短了你的数组:

a = np.random.exponential(1, [10, 100])  # of shape (10, 100)
b = np.random.exponential(1, [10, 100])
ind=np.argmax(a,axis=0)

使用 ind 到 select 在 ab 中每列一行:

a_ = a[ind,np.arange(a.shape[1])]
b_ = b[ind,np.arange(a.shape[1])]

然后计算c:

c=np.minimum(a_, b_)