What is the difference between backward slash vs forward slash in MATLAB?
244 views (last 30 days)
Show older comments
I have a failry simple question in MATLAB. What s the difference between the backslash operator vs the forward slash operator. For example,
A=[4,12;6,8];
b=[6,12;14,8];
x1 = A/b;
1.1333 -0.2000
0.5333 0.2000
%which is different from:
x2 = A\b;
3.0000 0
-0.5000 1.0000
I am asking because I am trying to convert a simple line of code from MATLAB to c++ which it turns out there's no forward slash in c++ unfortunately.
2 Comments
Torsten
on 19 Jun 2022
Of course there is a forward slash in c++. What else should stand for "division" ?
Answers (3)
Steven Lord
on 20 Jun 2022
A=[4,12;6,8];
b=[6,12;14,8];
x1 = A/b
check = x1*b-A
x2 = A\b
check = A*x2 - b
John D'Errico
on 20 Jun 2022
BOTH of them are linear algebraic solutions. Where matrices are involved, they solve subtly different problems.
A\b solves the linear algebra problem A*X=b.
For these matrices...
A=[4,12;6,8];
b=[6,12;14,8];
X1 = A\b
Did it work?
A*X1
Did it recover the matrix b? Yes.
What does forward slash do? Again, when matrices are involved, it solves a different problem. A/b is equivalent to solving the linear algebra problem X2*b=A.
X2 = A/b
Did it work?
X2*b
Essentially, the two are similar in philosophy. The difference is where the unknown matrix would be in the problem you are implicitly solving.
William Rose
on 20 Jun 2022
X=A\B computes X=inv(A)*B.
Y=A/B computes Y=A*inv(B)
A=rand(2,2); B=rand(2,2);
X1=A\B
X2=inv(A)*B
Y1=A/B
Y2=A*inv(B)
When I look at A\B, I try to remember that the A looks like it is "under" the divide sign, which reminds me that A is the denominator in A\B. And it comes first, so inv(A) is before B in the (non-commutative) multiplication.
7 Comments
Stephen23
on 20 Jun 2022
Edited: Stephen23
on 20 Jun 2022
" Matlab's left divide may not use the equation I gave above - @John D'Errico says it doesn't, and I trust him."
Even better is to read the MLDIVIDE() documentation yourself:
"The equation I gave in my comment (not my original answer) is standard in a statistics class when discussing linear regression."
But almost completely useless when doing numeric computations on numeric data.
In much the same way beginners use the determinent to test if a matrix is singular or not, because that is what they were taught in "statistics class", but in the real world of numeric computing: almost completely useless.
"But A'A is not necessarily invertible (although I have never encoutered a linear regression problem where it's not)."
Whether A'A is invertible is not really the problem here, this still avoids the numeric issue.
"So maybe Matlab has a way to deal with that possibility, by not using inv() when it does left matrix divide."
The MLDIVIDE() documentation explains what algorithms it uses. Read it.
See Also
Categories
Find more on Arithmetic Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!