Matlab backslash operator in python

For under-determined systems such as yours (rank is less than the number of variables), mldivide returns a solution with as many zero values as possible. Which of the variables will be set to zero is up to its arbitrary choice.

In contrast, the lstsq method returns the solution of minimal norm in such cases: that is, among the infinite family of exact solutions it will pick the one that has the smallest sum of squares of the variables.

So, the "special" solution of Matlab is somewhat arbitrary: one can set any of the three variables to zero in this problem. The solution given by NumPy is in fact more special: there is a unique minimal-norm solution

Which solution is better for your purpose depends on what your purpose is. The non-uniqueness of solution is usually a reason to rethink your approach to the equations. But since you asked, here is NumPy code that produces Matlab-type solutions.

import numpy as np
from itertools import combinations
A = np.matrix([[1 ,2, 0],[0, 4, 3]])
b = np.matrix([[8],[18]])

num_vars = A.shape[1]
rank = np.linalg.matrix_rank(A)
if rank == num_vars:              
    sol = np.linalg.lstsq(A, b)[0]    # not under-determined
else:
    for nz in combinations(range(num_vars), rank):    # the variables not set to zero
        try: 
            sol = np.zeros((num_vars, 1))  
            sol[nz, :] = np.asarray(np.linalg.solve(A[:, nz], b))
            print(sol)
        except np.linalg.LinAlgError:     
            pass                    # picked bad variables, can't solve

For your example it outputs three "special" solutions, the last of which is what Matlab chooses.

[[-1. ]
 [ 4.5]
 [ 0. ]]

[[ 8.]
 [ 0.]
 [ 6.]]

[[ 0.        ]
 [ 4.        ]
 [ 0.66666667]] 

Found an image of OP:

; ; ; ; ; ; ; ; ; ; ; ; ,/:::::::::::::::::::::::::::::::::::::::\,; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ,/:::::::::::::::::::::::::::::::::::::::::\,; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ,/:::::::::::::::::::::::::::::::::::::::::::::\,; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ,/::::::::::::::::::;;;;;;;;;;;::::::::::::::::::::\, ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ;,/:::::::::::::::::::;;;;;;;;;;;;;;:::::::::::::::: :::\, ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; __; ; ,/:::::::::::::::::::;;;;;;;;;;;;;;;;;;;::::::::::: ::::::\,; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ,~:::::/:::::::::::::::::::::::,______;;;;::::::::::::::: ::::`\,; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; |::::::::::::|:::::::::::::::::::::::/::---.,|::::|::::\:::::::::::::`\, ; ; ; HEEEEEY; ; ; ; ; ; ; ; ; ; ; ; ;
; |::::::::::::|:::::::::::::::::::::::\___O__ /::::/::::::\::::::::::::`\,; ; ; ; ; ;YOOOUUUU; ; ; ; ; ;
; ;-,:::::::::|::::::::::::::::::::::::::::::::::::_,,/:::::::::|:::,___:::`\;_; ; ; ; ; ; GUUUUUYYYS; ;
; ; ; -,::::::|:::::::::::::::::::::::::::::::::/::::::::::::::\::\,,O\:::|::|; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ;`-,:::|::::::::::::::::::::::::::::::::\,_:o:___:o__ \::::-,__|::|::/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; -,|::::::::::::::::::::::::::::::,/`:::::::::::::::::::/::::::::::: |:/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; \:::::::::::::::::::::::::::,/:::,-~~~-,::\::::::::::::|/ ; ; ; ; ; WHAT'S; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ;|:::::::::::::::::::::::::::|::/U|_|`\::\::::::::::/ ; ; ; ; ; ;GOING ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ;\::::::::::::::::::::::::::|/: : : : : : : :|:::|::::::::/; ; ; ; ; ; ; ; ; ; ;ON ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ;\:::::::::::::::::::::::::|\,: :,_ : : : :_: /::/:::::::/ ; ; ; ; ; ; ; ; ; ; ; IN; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; \::::::::::::::::::::::::|:`\~-| |____| |/::/:::::::/ ; ; ; ; ; ; ; ; ; ; ;; ;THIS ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ,/:::::::::::::::::::::::::\:::\,,_____,/::/:::::::/ ; ; ; ; ; ; ; ; ; ; ; ;; ;PYTHONZ? ; ;
; ; ; ; ; ; ; ; ; /:::::::::::::::::::::::::::::\___,,,,,,__/::::::::/ ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ;`~--,,_::::::::::::::::::::::::::::::::::::::::::::/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;`~--,::::::::::::::::::::::::::::::::::::/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; `~,::::::::::::::::::::::::::::::/ ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; `~,_:::::::::::::::::::::::/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; `~,::::::::::::::::::/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; `~-_______/; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;

1 year ago # QUOTE 0 Volod 1 Vlad !

What is the Matlab backslash operator?

Matlab Backslash Operator. MATLAB backslash operator is used to solving a linear equation of the form a*x = b, where 'a' and 'b' are matrices and 'x' is a vector. The solution of this equation is given by x = a \ b, but it works only if the number of rows in 'a' and 'b' is equal.

What is NumPy Matlab?

NumPy contains both an array class and a matrix class. The array class is intended to be a general-purpose n-dimensional array for many kinds of numerical computing, while matrix is intended to facilitate linear algebra computations specifically. In practice there are only a handful of key differences between the two.

What is the Python equivalent of in Matlab?

NumPy (Numerical Python) 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.

How do I import NumPy into Matlab?

Direct link to this answer.
Install Python (Cpython) from python.org/downloads..
Make sure the version you download is 64-bit if your Matlab exe is 64-bit. ... .
Open python and try the command "import numpy as np". ... .
Now you will be able to use numpy library in matlab..