Search code examples
pythonmatlabtranslation

Matlab VectorA/VectorB to Python


I'm currently translating my matlabcode to python and i have a line in Matlab like

X = VectorA/VectorB;

with x being a single scalar value and VectorA and VectorB are each arrays with the size 1x1750. But i can't manage to translate this into python code, I know it's a linear equation system that should work via numpy.linalg.solve(a, b) in python, but it wants b to be a square matrix and doesnt work. Whether I'm not using the correct function and/or mess up the syntax, in dont know.

Can you guys help me in this?


Solution

  • I think you might be looking for np.linalg.lstsq (https://numpy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html#numpy.linalg.lstsq).

    a = np.ndarray((2,1), buffer=np.array([1, 2, 3]), dtype=int)
    b = np.ndarray((2,1), buffer=np.array([2, 4, 6]), dtype=int)
    
    # Solve for X = b/a, or a * X = b
    X, resid, rank, s = np.linalg.lstsq(a, b, rcond=None)  # X = array([[2.]])