user423805 - 4 years ago 312
Python Question

Left Matrix Division and Numpy Solve

I am trying to convert code that contains the \ operator from Matlab (Octave) to Python. Sample code

``````B = [2;4]
b = [4;4]
B \ b
``````

This works and produces 1.2 as an answer. Using this web page

http://mathesaurus.sourceforge.net/matlab-numpy.html

I translated that as:

``````import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print lin.solve(B,b)
``````

This gave me an error:

``````numpy.linalg.linalg.LinAlgError: Array must be square
``````

How come Matlab \ works with non square matrix for B?

Any solutions for this?

From MathWorks documentation for left matrix division:

If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. In other words, X minimizes norm(A*X - B), the length of the vector AX - B.

The equivalent in numpy is np.linalg.lstsq:

``````In [15]: B = np.array([[2],[4]])

In [16]: b = np.array([[4],[4]])

In [18]: x,resid,rank,s = np.linalg.lstsq(B,b)

In [19]: x
Out[19]: array([[ 1.2]])
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download