Increment Numpy multi-d array with repeated indices

后端 未结 2 818
春和景丽
春和景丽 2020-12-18 11:18

I\'m interested in the multi-dimensional case of Increment Numpy array with repeated indices.

I have an N-dimensional array and a set N index arrays, who\'s values I

相关标签:
2条回答
  • 2020-12-18 11:23

    How about this:

    import numpy as np
    a = np.zeros((2,3,4))
    i = np.array([0,0,1])
    j = np.array([0,0,1])
    k = np.array([0,0,3])
    
    ijk = np.vstack((i,j,k)).T
    H,edge = np.histogramdd(ijk,bins=a.shape)
    a += H  
    
    0 讨论(0)
  • 2020-12-18 11:44

    I don't know if there is an easier solution with direct array indexing, but this works:

    for x,y,z in zip(i,j,k):
        a[x,y,z] +=1
    
    0 讨论(0)
提交回复
热议问题