Finding a vector that is orthogonal to n columns of a matrix

Viewed 245

Given a matrix B with shape (M, N), where M > N. How to find a vector v (with shape of M) that is perpendicular to all columns in B.

I tried using Numpy numpy.linalg.lstsq method to solve : Bx = 0. 0 here is a vector with M zeros.

It returns a vector of zeros with (N,) shape.

2 Answers

You can use sympy library, like

from sympy import Matrix
B = [[2, 3, 5], [-4, 2, 3], [0, 0, 0]]
V = A.nullspace()[0]

or to find whole nullspace

N = A.nullspace()

Here is what worked for me in case someone else need the answer:

u,s,vh=np.linalg.svd(B)
v=vh[-1:1]
Related