Error converting Matlab code to non-transferable Python output

I have the following code in Matlab

a= a + b(c,:);

      

where 'a' is 4524x3 matrix, 'b' is 1131x3 and 'c' is 4524x1.

In Python I have

a[:]+= b[c, :]

      

Where I get 'a' as 4524x4524x3 matrix. Why does Python create this extra dimension instead of summing the values?

+3


source to share


1 answer


Try this instead:

a[:] += b[c.ravel(), :]

      



Which is happening since it is c

treated as a two dimensional matrix and not one one dimensional array, so unnecessary broadcasting happens. You are basically trying to index a matrix with a 2D array when you need it to be 1D.

+4


source







All Articles