Is A./B different from B.\A?
23 views (last 30 days)
Show older comments
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
Accepted Answer
James Tursa
on 17 Jun 2015
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 Comments
Bruno Luong
on 30 Sep 2021
Edited: Bruno Luong
on 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
on 29 Oct 2021
Edited: James Tursa
on 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.
More Answers (2)
Alberto
on 17 Jun 2015
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.
on 28 Sep 2021
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 Comments
Bruno Luong
on 29 Sep 2021
Edited: Bruno Luong
on 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
A*pinv(B)
but when rank(b) < size(b,1) such formula is not correct
B=rand(4,2);
A=rand(3,2);
A/B
A*pinv(B)
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
A1=A(1,:); x1=dot(A1,b)/dot(b,b)
A2=A(2,:); x2=dot(A2,b)/dot(b,b)
A3=A(3,:); x3=dot(A3,b)/dot(b,b)
% Or all together
x=A*b'/(b*b')
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).
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!