Raiyan Chowdhury
Raiyan Chowdhury

Reputation: 311

Subtract Vector from Every Column of a Matrix

Suppose I have an n x 1 column vector v, and an n x m matrix M. I'm looking for a method to subtract v from every column of M without a loop in Numpy. How can I do this?

I've searched the web and I can't find a method to do this.

Upvotes: 1

Views: 2292

Answers (1)

tenhjo
tenhjo

Reputation: 4537

Besides searching the web most of the time it is useful to just play around with the arrays and see what works. In you case it is really straight forward:

import numpy as np

n, m = 13, 17
v = np.random.random((n, 1))
M = np.random.random((n, m))

res = M - v

This is also a good resource to get familiar with the basic concepts of numpy.

Upvotes: 2

Related Questions