Addition of non-uniform dimensional matrices
3 views (last 30 days)
Show older comments
While writing a code for my research paper, the below Matlab operation since did not gave an error, I wasn't able to get the desired results. As per mathematics rules, for addition and subtraction, the 2 matrices should have the same dimensions.
1 - Why Matlab is not giving an error for addition or subtraction of below matrices?
2 - What is the advantage of allowing such an operation?
A=[1, 2, 3, 4];
B = [1; 2; 3;4];
C = A - B is calculated by Matlab.
Similarly the below operation also doesn't land into any errors, something strange.
D = [1, 2, 3, 4];
E = [1; 2; 3];
F = D-E;
G = E-D
0 Comments
Answers (2)
Walter Roberson
on 2 Feb 2018
"As per mathematics rules, for addition and subtraction, the 2 matrices should have the same dimensions."
No, what mathematics tell us that if 2 matrices do have the same dimensions, that the result of those operations should be such-and-such. Mathematics has nothing to say about the outcome of those operations on two dissimilar arrays.
Including that mathematics has nothing to say about the outcome of (for example) 1 + [3, 4, 5], which is the addition of two dissimilar arrays. It is convenient that MATLAB automatically interprets this as 1*[1, 1, 1] + [3, 4, 5] . And sometimes it is convenient that MATLAB automatically interprets [1; 2] + [3, 4, 5] as [1; 2] * [1 1 1] + [1;1] * [3, 4, 5]
The change to operations does not affect any calculation for which the array dimensions does match. The change affects only calculations for which the array dimensions does not match. And the change is that it now gives a meaningful and not unreasonable result instead of giving an error message.
There are times when I am writing code when I wish the operations would give an error message for unmatched dimension, that perhaps they had given a different form to the new functionality, such as calling it #+ and $- or something like that -- times when I do not expect the operation to be taking place and want the debugging help. But that does not mean that the new operations are wrong in any way, just that the interface to them is not optimal taking into account that size mismatches are somewhat common and automatic expansion is not always desirable.
0 Comments
Star Strider
on 30 Jan 2018
‘1 - Why Matlab is not giving an error for addition or subtraction of below matrices?’
Beginning in R2016b, MATLAB automatically expands matrix arguments. Previously, this required bsxfun (that I still prefer, and that I use in my Answers here).
‘2 - What is the advantage of allowing such an operation?’
You have ow joined many of us here who asked the same thing in the last year or so.
7 Comments
Star Strider
on 2 Feb 2018
@Ihsan Ullah — Thank you for your efforts in having MathWorks reconsider this. A ‘switch’ to turn off ‘implicit expansion’ would be welcome.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!