I have a [n x n] matrix containing values belonging to different groups, and a [1 x n] vector defining to which group each element belongs. (n usually ~1E4, in this example n=4)
I want to calculate a matrix obtained by summing together all the elements belonging to the same group.
I use np.where() to calculate the indices where the elements of each group are located. When I use the calculated indices, I do not obtain the expected elements, because I select pairs of positions instead of ranges (I'm used to Matlab, where I can simply select M(idx1,idx2) ).
import numpy as np
n=4
M = np.random.rand(n,n)
print(M)
# This vector defines to which group each element belong
belongToGroup = np.array([0, 1, 0, 2])
nGroups=np.max(belongToGroup);
# Calculate a matrix obtained by summing elements belonging to the same group
M_sum = np.zeros((nGroups+1,nGroups+1))
for g1 in range(nGroups+1):
idxG1 = np.where(belongToGroup==g1)
for g2 in range(nGroups+1):
idxG2 = np.where(belongToGroup==g2)
print('g1 = ' + str(g1))
print('g2 = ' + str(g2))
print(idxG1[0])
print(idxG2[0])
print(M[idxG1[0],idxG2[0]])
print(np.sum(M[idxG1[0],idxG2[0]]))
M_sum[g1,g2]=np.sum(M[idxG1[0],idxG2[0]])
print('')
print('Example of the problem:')
print('Elements I would like to sum to obtain M_sum[0,0]')
print(M[0:2,0:2])
print('Elements that are summed instead')
print(M[[0,1],[0,1]])
Example of the problem: In the example above, the element M_sum[0,0] should be the sum of M[0,0], M[0,1], M[1,0], and M[1,1] Instead, it is calculated as the sum of M[0,0] and M[1,1]