How do I find least square between two matrix in MATLAB?

242 Views Asked by At

What is the replacement in MATLAB for the following line of code snippet in python? From Python Implementation for SIFT Feature Extraction

 x = -lstsq(hessian, gradient, rcond=None)[0]

if

hessian = [-0.001 -9.042 -9.491;-9.042 -2.345 -7.983;-9.491 -7.983 -7.269] and gradient = [1.6 6.1 9.3]

The following is what is implemented currently in MATLAB but gives a localization error for SIFT Feature Extraction

[U,S,V] = svd(hessian); %singular value decomposition for eigenvectors 
T=S;
T(S~=0) = 1./S(S~=0);  
invH = V .* T' .* U'; %inverse hessian
x = - invH.*gradient;
1

There are 1 best solutions below

0
Cris Luengo On
x = numpy.linalg.lstsq(A, B)[0]

is the solution to the linear equation Ax=B. If over- or underdetermined, it returns the least squares solution.

In MATLAB you compute this solution with

x = A\B;

See the documentation.

To explicitly use a least squares solver, use lsqr, this is typically useful only for sparse matrices:

x = lsqr(A, B);