mldivide returns matrix with multiple rows = 0 when they should not be
3 views (last 30 days)
Show older comments
I'm trying to solve the equation Ax=B where A is a 1-by-m matrix and B is a 1-by-n matrix. By the rules of linear algebra I know that this means that x should be a m-by-n matrix. When I use the \ operator, I get a correctly-sized matrix, except that only one row contains non-zero numbers. If I instead write a for loop to \ each element of A individually into the entire B matrix, I get the correct answer. For example:
EDU>> A=1:4;
EDU>> B=1:6;
EDU>> x=A\B
x =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0.2500 0.5000 0.7500 1.0000 1.2500 1.5000
EDU>> for m = length(A):-1:1
x(m,:) = A(m)\B;
end
EDU>> x
x =
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000
0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
0.3333 0.6667 1.0000 1.3333 1.6667 2.0000
0.2500 0.5000 0.7500 1.0000 1.2500 1.5000
I have tried this several times, with different sized A and B matrices. In some instances the first row of x is correct, and the remaining rows are zero, while in others (as above) the last row of x is correct, and the remaining rows are zero. Can anyone explain why this is happening?
0 Comments
Answers (2)
Walter Roberson
on 9 Dec 2013
bxsfun(@rdivide, B.', A)
or
bxsfun(@ldivide, A.', B)
x = A\B solves the system of linear equations A*x = B.
That is quite different than the output that you want for output.
Roger Stafford
on 9 Dec 2013
Lauren, your equations are highly underdetermined. Given the sizes of A and B, you necessarily have six equations and twenty-four unknowns. In each equation there are four unknowns which don't appear anywhere else. Consequently in each equation matlab is free to arbitrarily set three of them to zero and solve for the fourth one. It could have set three of them to any other arbitrary values and solved for the fourth. Why do you find this surprising? What else would you expect for a solution? If you want unique solutions you need in general to have an equal number of equations and unknowns.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!