In python numpy:
A = [[1,2,3],[4,5,6],[7,8,9]]
b = [2,3,5]
want [1,2,3] -2, [4,5,6]-3, [7,8,9] -5
e.g. ideal result:
[[-1,0,1],[1,2,3],[2,3,4]]
any way solve this without loop?
In python numpy:
A = [[1,2,3],[4,5,6],[7,8,9]]
b = [2,3,5]
want [1,2,3] -2, [4,5,6]-3, [7,8,9] -5
e.g. ideal result:
[[-1,0,1],[1,2,3],[2,3,4]]
any way solve this without loop?
You have not used Numpy at all. It is pretty easy with that. You need to add an extra dimension to b with None or numpy.newaxis and then subtract.
import numpy
A = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
b = numpy.array([2,3,5])
c= A-b[:,None]
print(c)
Output:
[[-1 0 1]
[ 1 2 3]
[ 2 3 4]]