3

I have the following code in Matlab

a= a + b(c,:);

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

In Python I have

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

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

1
  • What are the exact dimensions of c? What happens when you do c.shape in the command prompt? Do you get (4524,) or (4524,1)? Commented Jun 5, 2015 at 17:59

1 Answer 1

4

Try doing this instead:

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

What's happening is that c is considered as a two-dimensional matrix rather than a single 1D array, which is why the unnecessary broadcasting is happening. You are basically trying to index the matrix with a 2D array when you need it to be 1D.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.