Given two matrices, A and B, will A./B ever give a different answer from B.\A, or are these two expressions equivalent?
It seems that even for complex numbers they return the same thing. E.g.
>> A = sqrt(randn(3));
>> B = sqrt(randn(3));
>> isequal(A./B, B.\A)
ans = 1

3 commentaires

Stephen23
Stephen23 le 17 Juin 2015
Modifié(e) : Stephen23 le 17 Juin 2015
>> 2./3
ans =
0.6667
>> 2.\3
ans =
1.5000
The documentation clearly states what each function does:
Ingrid
Ingrid le 17 Juin 2015
Stephen, what he is asking is something else namely
>> 2./3
ans =
0.6667
>> 3.\2
ans =
0.6667
so I think they will always give the same result
Stephen23
Stephen23 le 17 Juin 2015
According to the documentation A.\B and B./A are the same:
  • ldivide: " B.\A divides each element of A by the corresponding element of B"
  • rdivide: " A./B divides each element of A by the corresponding element of B"
Unless the definition of "divide" is different, then these should be the same.

Connectez-vous pour commenter.

 Réponse acceptée

James Tursa
James Tursa le 17 Juin 2015

0 votes

I can't think of any reason why one would ever get different results for numeric types. I suppose there might be speed differences if one form used multi-threading and the other form didn't, but in tests I just ran they both appeared to take about the same amount of time.
User defined classes could of course overload them differently.

6 commentaires

Bruno Luong
Bruno Luong le 28 Sep 2021
Not sure but A, B contains non-commutative algebra elements such as quaternion, the results of A./B abd B./A must be different.
James Tursa
James Tursa le 29 Sep 2021
Modifié(e) : James Tursa le 29 Sep 2021
For non-commutative algebra such as quaternions, yes it would depend on whether the division operators were overloaded or not, and how they were overloaded per my comment above. So if A./B were overloaded as A*inv(B) and B.\A were overloaded as inv(B)*A, then yes you could get different results.
To reiterate the point I made in my Answer, any class that overloads the division operators differently could get different results.
To be clear I don't refer to specific class and user overload (which he/she can define anything he/she wants).
Rather the algebric definition and difference between A./B and B.\A; accrding to TMW:
The first (A./B) should return X such that
X.*B = A
The second (B.\A) should return Y such that
B.*Y = A
where .* represents element-wise multiplication of (nd) array A and B (assumed 2D and without auto-expansion for simplification of the discussion).
So for quaternion
X(i,j) = A(i,j) .* einv(B(i,j))
Y(i,j) = einv(B(i,j)) .* A(i,j)
einv here defines element-wise inverse of array B. For non-commutative algebra X and Y are then different in general.
I imagine in some even more exotic algebra, left-inverse and right-inverse can be different (not for quaternion case).
James Tursa
James Tursa le 29 Sep 2021
Modifié(e) : James Tursa le 29 Sep 2021
"... I don't refer to specific class and user overload ..."
But to my thinking this must be the case, at least in MATLAB. None of the numeric intrinsic types are non-commutative for multiplication at the element-wise level, so left or right element-wise division is not ambiguous in this sense. The only way to get non-commutative objects such as quaternions is to create a class for them, and once you do that you are into the realm of overloaded operations. I.e., the only MATLAB discussion to be had for non-commutative objects must involve created classes which means overloading functions and operators.
If you are not restricting the discussion to MATLAB, then I suppose you would need to know how the language handled left vs right division operators.
Bruno Luong
Bruno Luong le 30 Sep 2021
Modifié(e) : Bruno Luong le 30 Sep 2021
"The only way to get non-commutative objects such as quaternions is to create a class for them"
But they are available in some toolboxes, e.g., https://www.mathworks.com/help/robotics/ref/quaternion.html
I don't have any of those toolboxes to check how "A./B" and "B.\A" works, but I expect them NOT give the same results for general cases:
James Tursa
James Tursa le 29 Oct 2021
Modifié(e) : James Tursa le 29 Oct 2021
Yes, your expectations are correct. For the MATLAB toolbox quaternion class objects, the q./p and p.\q operations are implemented as expected by multiplying by the inverse, and since multiplication is non-commutative you get different results.
>> x = rand(1,4)-0.5; x = x/norm(x); q = quaternion(x);
>> x = rand(1,4)-0.5; x = x/norm(x); p = quaternion(x);
>> q
q =
quaternion
-0.62168 + 0.46748i + 0.58112j + 0.23933k
>> p
p =
quaternion
0.64169 + 0.60532i - 0.26832j + 0.38709k
>> q./p
ans =
quaternion
-0.17923 + 0.38713i + 0.24217j + 0.87141k
>> p.\q
ans =
quaternion
-0.17923 + 0.96545i + 0.17j - 0.082977k
>> q*conj(p)
ans =
quaternion
-0.17923 + 0.38713i + 0.24217j + 0.87141k
>> conj(p)*q
ans =
quaternion
-0.17923 + 0.96545i + 0.17j - 0.082977k
>> which quaternion
C:\Program Files\MATLAB\R2020a\toolbox\shared\rotations\rotationslib\@quaternion\quaternion.m % quaternion constructor
Note that the / and \ operators are not implemented for this class:
>> q/p
Error using /
Arguments must be numeric, char, or logical.
>> p\q
Error using \
Arguments must be numeric, char, or logical.

Connectez-vous pour commenter.

Plus de réponses (2)

Alberto
Alberto le 17 Juin 2015

0 votes

Both are pointwise, but A./B divides every element in A by the same element in B. A.\B divides every element in B by the same element in A.
H. Sh. G.
H. Sh. G. le 28 Sep 2021

0 votes

Hi every body.
I wonder what kind of calculations the division of a matrix (X) by a row vector (y), i.e. X/y, does, where both have the same number of columns.
The result is a column vector of the same number of rows that X has.
Recall that X./y divides all elements of each column in X by the element of y in the same column, resulting in a matrix with the same size of X.

4 commentaires

Stephen23
Stephen23 le 28 Sep 2021
"I wonder what kind of calculations the division of a matrix (X) by a row vector (y), i.e. X/y, does, where both have the same number of columns."
The documentation explains what MRDIVIDE does:
H. Sh. G.
H. Sh. G. le 28 Sep 2021
Hi Stephen,
Thank you for the reference.
Yes, I know that b/A gives pinv(A)*b, but what if I change the places and divide A/b, where b is a row vector?
I checked and it does the same type of calculations; i.e. x = A/b is a solution to b*x = A.
Indeed, a pseudo inverse of the row vector is calculated by the operator.
However, x*b does not result in A, if x is the (a) solution to the equation above.
For example:
A = [1 1 3; 2 0 4; -1 6 -1];
b = [2 19 8];
The equation x*A = b can be solved by x = b/A =>
x = b/A
x = 1×3
1.0000 2.0000 3.0000
Now:
x = A / b
0.1049
0.0839
0.2424
which is equal to A * pinv(b). Note: we know that the solution may not be a unique one.
But:
x*b gives:
0.2098 1.9930 0.8392
0.1678 1.5944 0.6713
0.4848 4.6061 1.9394
Different from A.
Thanks in advance for further clarifcations,
Hamed.
Bruno Luong
Bruno Luong le 29 Sep 2021
Modifié(e) : Bruno Luong le 30 Sep 2021
"Yes, I know that b/A gives pinv(A)*b."
It's incorrect. In some cases b/A is A*pinv(b) (and not the opposite as you wrote)
B=rand(2,4);
A=rand(3,4);
A/B
ans = 3×2
0.3160 0.6389 -0.4427 0.6439 2.4027 -0.5932
A*pinv(B)
ans = 3×2
0.3160 0.6389 -0.4427 0.6439 2.4027 -0.5932
but when rank(b) < size(b,1) such formula is not correct
B=rand(4,2);
A=rand(3,2);
A/B
ans = 3×4
1.2479 0 0 0.0022 -0.2076 0 0 1.0500 0.1178 0 0 1.4824
A*pinv(B)
ans = 3×4
1.1939 -0.2063 0.1426 0.0418 -0.1644 0.3918 0.3595 0.5231 0.1610 0.4850 0.5542 0.7518
Now to your question.
In case B = b is a row vector, let consider the system
% x*B = A;
x is column vector (since is a row vector). This system works row by row of x and A independenty. So consider a row equation
% x(i) * b = A(i,:).
So what you ask is which scalar x(i) that when multiplying with a vector (b) must be equal to another vector A(i,:). Such solution does not exist unless A(i,:) is proportional to b. In general MATLAB returns the least square solution:
% x(i) = dot(A(i,:),b) / dot(b,b)
Illustration test :
b=rand(1,4);
A=rand(3,4);
A/b
ans = 3×1
0.4635 0.8082 0.3771
A1=A(1,:); x1=dot(A1,b)/dot(b,b)
x1 = 0.4635
A2=A(2,:); x2=dot(A2,b)/dot(b,b)
x2 = 0.8082
A3=A(3,:); x3=dot(A3,b)/dot(b,b)
x3 = 0.3771
% Or all together
x=A*b'/(b*b')
x = 3×1
0.4635 0.8082 0.3771
Note that for a row vector b, pinv(b) is b'/(b*b').
And x*B will not match A, unless all the rows of A are proportional to b (which is a strong coincidence in general).
H. Sh. G.
H. Sh. G. le 29 Sep 2021
Thanks Bruno,
This explains my question well.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by