I have the following 2D-array:
a = array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15]])
You need to convert array b to a (2, 1) shape
array, use None or numpy.newaxis
in the index tuple. Here is the Indexing of Numpy array.
You can do it Like:
import numpy
a = numpy.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15]])
b = numpy.array([ 1, 2, 3, 4, 5])
c=a - b[:,None]
print c
Output:
Out[2]:
array([[ 0, 1, 2],
[ 2, 3, 4],
[ 4, 5, 6],
[ 6, 7, 8],
[ 8, 9, 10]])