What is the difference between backward slash vs forward slash in MATLAB?

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

Of course there is a forward slash in c++. What else should stand for "division" ?
Well, I am talking about more like "matrix" division using Eigen library which seems like not working.

Sign in to comment.

Answers (3)

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
X1 = 2×2
8.5138 7.1476 -3.5676 -2.2030
X2=inv(A)*B
X2 = 2×2
8.5138 7.1476 -3.5676 -2.2030
Y1=A/B
Y1 = 2×2
0.2325 -0.2566 -0.0595 0.7033
Y2=A*inv(B)
Y2 = 2×2
0.2325 -0.2566 -0.0595 0.7033
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

Um, ABSOLUTELY NOT TRUE. It is not true that inv is used! INV IS NOT USED AT ALL, in any of those calls.
Note that if the matrices are not square, then inv would fail. Regardless, inv is NOT used. Period.
X=A\B finds the solution X to the equation A*X=B.
Y=A/B finds the solution Y to the equation Y*B=A.
In the examples I gave, A and B were the same size, but they don't have to be. They just have to have compatible dimensions for matrix multplication. Or the denominator can be a scalar, in which case simple division-by-a-scalar occurs.
Thanks A LOT! This solves my problem in C++.
@Jamie Al, you;re welcome. @John D'Errico is right. Thank you, @John D'Errico. for catching my mistake. You can't invert a non-square matrix, but matrix divide works even with non-square matrices. So it is more complicated. For example, the matrix equation Ax=b arises in least-squares fitting, and A is non-square, so it cannot be inverted. Therefore you do the following:
Ax=b
A'Ax=A'b (where prime means transpose)
inv(A'A)(A'A)x = inv(A'A)(A'b)
x=inv(A'A)(A'b)
Therefore x=A\B can be attained with x=inv(A'A)A'B, even if A is non-square.
A=rand(3,2); B=rand(3,1);
X1=A\B, X2=inv(A'*A)*A'*B
X1 = 2×1
0.0639 1.4522
X2 = 2×1
0.0639 1.4522
Good luck.
@Jamie Al, Matlab's left divide may not use the equation I gave above - @John D'Errico says it doesn't, and I trust him. The equation I gave in my comment (not my original answer) is standard in a statistics class when discussing linear regression. It works because A'A is guaranteed to be square, even if A is not. But A'A is not necessarily invertible (although I have never encoutered a linear regression problem where it's not). So maybe Matlab has a way to deal with that possibility, by not using inv() when it does left matrix divide.
You use the same idea for right divide, Y=A/B, which solves the matrix equation YB=A:
YB=A => Y(BB')=AB' => Y(BB')inv(BB')=AB'inv(BB') => Y=AB'inv(BB')
Therefore Y=A/B can be attained with Y=A*B'*inv(B*B'), as long as BB' is invertible.
@William Rose @John D'Errico Thanks all for all the help, I greatly appreciate it. For now inv seems to work for my square matrices but I will keep in mind what you discussed here as well.
" 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.

Sign in to comment.

A=[4,12;6,8];
b=[6,12;14,8];
From the documentation for mrdivide, /, if x = A/b then x*b should be close to A.
x1 = A/b
x1 = 2×2
1.1333 -0.2000 0.5333 0.2000
check = x1*b-A
check = 2×2
0 0 0 0
Similarly from the documentation for mldivide, \, if x = A\b then A*x should be close to b.
x2 = A\b
x2 = 2×2
3.0000 0 -0.5000 1.0000
check = A*x2 - b
check = 2×2
1.0e-15 * 0.8882 0 0 0
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
X1 = 2×2
3.0000 0 -0.5000 1.0000
Did it work?
A*X1
ans = 2×2
6.0000 12.0000 14.0000 8.0000
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
X2 = 2×2
1.1333 -0.2000 0.5333 0.2000
Did it work?
X2*b
ans = 2×2
4 12 6 8
Essentially, the two are similar in philosophy. The difference is where the unknown matrix would be in the problem you are implicitly solving.

Asked:

on 19 Jun 2022

Edited:

on 20 Jun 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!