Matlab (mldivide) backslash compared to numpy lstsq with rectangular matrix

I am trying to replicate the analysis in Python that we usually do with Matlab. A certain step involves solving the least squares problem with 2 rectangular matrices using the mldivide operator (for solving for x in Ax = b).

I noticed that in some cases Matlab produces very different results than np.linalg.lstsq. If my matrix Ahas no rank, then it A\bgives me a warning, but it also gives me an answer with some columns set to all zeros.

A = reshape(1:35,5,7);
b = [0.5, 0.4, 0.3, 0.2, 0.1]'

A\b
Warning: Rank deficient, rank = 2, tol =  1.147983e-13. 

ans =

    -0.1200
     0
     0
     0
     0
     0
     0.0200

, A. : mldivide Matlab (a.k.a. "\" ). , : Numpy vs mldivide, "\" matlab. , .

A , \ QR-, , :

[Q,R]  = qr(A);
R \ (Q'*b)
ans =

    -0.1200
     0
     0
     0
     0
     0
     0.0200

, \ ( R\(Q'*b)) . - Matlab python ? , .

, , , - , , .

, , mldivide over lstsq? , lstsq 2- , , , Matlab?

+4

All Articles