Addition of non-uniform dimensional matrices

3 views (last 30 days)
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

Answers (2)

Walter Roberson
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.

Star Strider
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
Ihsan Ullah
Ihsan Ullah on 2 Feb 2018
Thank you everyone for the interest shown here.
@Star Strider, I replied to Matlab technical support team earlier (combining your words with mine) and to reconsider the operation. I m sharing their response as below,
Hello Ihsan,
Thank you for the detailed response.
I acknowledge your point of view and agree with the concerns you have put forth regarding 'Implicit Expansion'. It would be useful for both new users and experts alike to be provided with a warning and if possible, to switch "off" this feature. Hence, I have relayed your feedback to the Development team and apprised them of your opinion and the difficulties this features introduces into your workflows. They may consider including these enhancements in a future release of MATLAB.
Thank you once again for your feedback and help in improving MATLAB!
Best,
RXXXXXX
Star Strider
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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!