1

Variable a has the shape (3,1) and variable b has the shape (3,100). Now, I want to add variable a to just one column of variable b, meaning:

x[:,ii] = a + b[:,ii]

However, I get this message:

could not broadcast input array from shape (3,3) into shape (3,)

What am I missing?

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Mino
  • 25
  • 3
  • Does this answer your question? [ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)](https://stackoverflow.com/questions/43977463/valueerror-could-not-broadcast-input-array-from-shape-224-224-3-into-shape-2) – NicoCaldo Jul 12 '22 at 11:35
  • By the rules of broadcasting `b[:,11]` is expanded from (3,) to (1,3). Add that to a (3,1) and we get a (3,3). The error comes from trying to put that into a (3,) slot. – hpaulj Jul 12 '22 at 15:36

2 Answers2

1

You need to use numpy.ravel() Because a.shape is (3,1) and you need (3,).

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


# Or By thanks `@Raphael` we can use `np.squeeze()`
# x[:,ii] = a.squeeze() + b[:,ii]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
-1

because the shape is 3 X 1 and 3x100, to multiply the column of first matrix and row of second matrix should be same(for eg. it would work it the matrix's are 3x1 X 1x100)