当处理维度(A,B,C)的三维矩阵“M”时,可以使用具有[0,A)中的元素的2个向量X和具有相同的[0,B]中的元素的Y来索引M.尺寸D.
更具体地说,我在写作时明白
M[X,Y,:]
我们正在为D中的每个“我”,
M[X[i], Y[i], :],
从而最终产生DxC矩阵 .
Now suppose
X is a numpy array of dim U, same concept as before
this time Y is a matrix UxL, where each row correspond to a Boolean numpy array
(a mask)
并查看以下代码
for u in U:
my_matrix[Y[u], X[u], :] += 1 # Y[u] is the mask that selects specific elements of the first dimension
I would like to write the same code without the for loop. 这样的事情
np.add.at(my_matrix, (Y, X), 1) # i use numpy.ufunc.at since same elements could occur multiple times in X or Y.
不幸的是,它返回以下错误
IndexError:布尔索引与维度0的索引数组不匹配; dimension是L但相应的布尔维数是1
执行分配时也可以找到此问题
for u in U:
a_matrix[u, Y[u], :] = my_matrix[Y[u], X[u], :]
你知道如何以优雅的方式解决这个问题吗?