Reputation: 3961
I want to subtract the first value of a row from the rest of the elements of that row, so for
import numpy as np
z = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,9]])
for n in range(0,3):
znew = z[n,:]-z[n,0]
znew
should be np.array([[0,1,2,3],[0,1,2,3],[0,1,2,2]])
. How can I do this? It kind of seems trivial.
Upvotes: 2
Views: 1515
Reputation: 251011
You can do this by taking advantage of broadcasting:
>>> z - z[:,0][:, None] # or z - z[:,0][:, np.newaxis]
array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 2]])
Upvotes: 3