Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array division- translating from MATLAB to Python

I have this line of code in MATLAB, written by someone else:

c=a.'/b

I need to translate it into Python. a, b, and c are all arrays. The dimensions that I am currently using to test the code are:

a: 18x1,
b: 25x18,

which gives me c with dimensions 1x25.

The arrays are not square, but I would not want the code to fail if they were. Can someone explain exactly what this line is doing (mathematically), and how to do it in Python? (i.e., the equivalent for the built-in mrdivide function in MATLAB if it exists in Python?)

like image 327
EmilyS Avatar asked Jun 16 '09 13:06

EmilyS


People also ask

How do you divide an array in Python?

divide() in Python. numpy. divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise).

How do you divide arrays in MATLAB?

Description. X = A ./ B performs right-array division by dividing each element of A by the corresponding element of B . X = rdivide( A , B ) is an alternative way to execute X = A./B .

Is matrix division possible in MATLAB?

Description. X = A / b performs right-matrix division. X = mrdivide( A , b ) is an alternative way to execute X = A/b .

Is NumPy similar to MATLAB?

NumPy arrays are the equivalent to the basic array data structure in MATLAB. With NumPy arrays, you can do things like inner and outer products, transposition, and element-wise operations.


2 Answers

The line

c = a.' / b

computes the solution of the equation c b = aT for c. Numpy does not have an operator that does this directly. Instead you should solve bT cT = a for cT and transpose the result:

c = numpy.linalg.lstsq(b.T, a.T)[0].T
like image 51
Vebjorn Ljosa Avatar answered Sep 22 '22 11:09

Vebjorn Ljosa


The symbol / is the matrix right division operator in MATLAB, which calls the mrdivide function. From the documentation, matrix right division is related to matrix left division in the following way:

B/A = (A'\B')'

If A is a square matrix, B/A is roughly equal to B*inv(A) (although it's computed in a different, more robust way). Otherwise, x = B/A is the solution in the least squares sense to the under- or over-determined system of equations x*A = B. More detail about the algorithms used for solving the system of equations is given here. Typically packages like LAPACK or BLAS are used under the hood.

The NumPy package for Python contains a routine lstsq for computing the least-squares solution to a system of equations. This routine will likely give you comparable results to using the mrdivide function in MATLAB, but it is unlikely to be exact. Any differences in the underlying algorithms used by each function will likely result in answers that differ slightly from one another (i.e. one may return a value of 1.0, whereas the other may return a value of 0.999). The relative size of this error could end up being larger, depending heavily on the specific system of equations you are solving.

To use lstsq, you may have to adjust your problem slightly. It appears that you want to solve an equation of the form cB = a, where B is 25-by-18, a is 1-by-18, and c is 1-by-25. Applying a transpose to both sides gives you the equation BTcT = aT, which is a more standard form (i.e. Ax = b). The arguments to lstsq should be (in this order) BT (an 18-by-25 array) and aT (an 18-element array). lstsq should return a 25-element array (cT).

Note: while NumPy doesn't make any distinction between a 1-by-N or N-by-1 array, MATLAB certainly does, and will yell at you if you don't use the proper one.

like image 40
gnovice Avatar answered Sep 18 '22 11:09

gnovice