python - Numpy replace for loop by using a matrix to perform indexing -


when dealing 3-dimensional matrix "m" of dimensions (a, b, c), 1 can index m using 2 vectors x elements in [0, a) , y elements in [0, b) of same dimension d.

more specifically, understand when writing

m[x,y,:] 

we taking, each "i" in d,

m[x[i], y[i], :], 

thus producing dxc matrix in end.

now suppose

x numpy array of dim u, same concept before time y matrix uxl, each row correspond boolean numpy array  (a mask) 

and @ following code

for u in u:     my_matrix[y[u], x[u], :] += 1  # y[u] mask selects specific elements of first dimension 

i write same code without loop. this

np.add.at(my_matrix, (y, x), 1) # use numpy.ufunc.at since same elements occur multiple times in x or y. 

which unfortunately returns following error

indexerror: boolean index did not match indexed array along dimension 0; dimension l corresponding boolean dimension 1

this issue can found when performing assignment

for u in u:     a_matrix[u, y[u], :] = my_matrix[y[u], x[u], :] 

do know how can address problem(s) in elegant way?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -