Question about A\b
11 views (last 30 days)
Show older comments
Hi everyone,
I really like A\b (just 3 letters and it can do magic). However, I run into this problem.
>> A = [1 1; 1 1]
A =
1 1
1 1
>> b = [1; 1]
b =
1
1
>> A\b
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
ans =
NaN
NaN
I understand that A is not invertible. However there is a solution and I kind of expect MATLAB will return [1; 0].
Or maybe I have the wrong expectation.
3 Comments
John D'Errico
on 9 Dec 2017
Edited: John D'Errico
on 9 Dec 2017
It looks like everyone in that same class will be asking this virtually identical question.
Answers (1)
Matt J
on 8 Dec 2017
Edited: Matt J
on 8 Dec 2017
It's really all a question of which linear solver you use. Each has its own idea of which of the infinite solutions to choose from. One alternative is,
>> pinv(A)*b
ans =
0.5000
0.5000
Another is,
>> lscov(A,b)
Warning: A is rank deficient to within machine precision.
> In lscov (line 200)
ans =
1.0000
0
3 Comments
Matt J
on 9 Dec 2017
Edited: Matt J
on 9 Dec 2017
You may have found a corner case, but LSCOV should always return the solution with a maximum number of zeros. From the documentation:
x = lscov(A,B) returns the ordinary least squares solution to the linear system of equations A*x = B, i.e., x is the n-by-1 vector that minimizes the sum of squared errors (B - A*x)'*(B - A*x), where A is m-by-n, and B is m-by-1. B can also be an m-by-k matrix, and lscov returns one solution for each column of B. When rank(A) < n, lscov sets the maximum possible number of elements of x to zero to obtain a "basic solution".
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!