Reputation: 36329
Consider the following:
>>> matrix = numpy.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>> vector = numpy.array([10, 20, 30])
>>> matrix + vector
array([[11, 22, 33],
[14, 25, 36],
[17, 28, 39]])
This adds the vector and the matrix row-wise (i.e. each row being added the vector).
How to perform the same column-wise? The result should be
>>> ???
array([[11, 12, 13],
[24, 25, 26],
[37, 38, 39]])
I'm aware that I can use
>>> (matrix.T + vector).T
array([[11, 12, 13],
[24, 25, 26],
[37, 38, 39]])
However I have many such additions and using this double transposition will make the code quite unreadable. Is there a way to configure ndarrays such that they will perform addition along the first axis (instead of the last)?
Upvotes: 1
Views: 6474