Question about A\b
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2017-12-9
编辑:John D'Errico
2017-12-9
It looks like everyone in that same class will be asking this virtually identical question.
回答(1 个)
Matt J
2017-12-8
编辑:Matt J
2017-12-8
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 个评论
Matt J
2017-12-9
编辑:Matt J
2017-12-9
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".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!