Are the rules for element by element matrix operation changed?

Alessandro on 13 Mar 2024
Latest activity Reply by Dyuman Joshi on 16 Mar 2024

Hi
I have Matlab 2015b installed and if I try:
ones(2,3,4)+ones(2,3)
of course I get an error. But my student has R2023b installed and she gets a 2x3x4 matrix as a result, with all elements = 2.
How is it possible?
Thanks
A
Dyuman Joshi
Dyuman Joshi on 16 Mar 2024
Why was this post transferred from Answers?
David Young
David Young on 14 Mar 2024
I'll just add that the introduction of singleton dimension expansion made me very happy. I'm still a bit pleased when I can replace bsxfun or repmat somewhere.
If it's a problem for students learning the language, then so will be broadcasting in Python and other languages. It's arguably better for students to learn early on of this convention.
John D'Errico
John D'Errico on 13 Mar 2024
Long ago, in a galaxy far, far away, we all used to use repmat to work with arrays of different sizes like that. Our code was good, and was blessed by the gods of MATLAB.
Then a good wizard named Loren helped us, providing us with the function bsxfun. It too was seen to be a good thing. But after some years of using bsxfun, the gods of MATLAB provided us with singleton dimension expansion. This happened with release R2016b. We all reveled in the beauty of our new tool, though even today some still lament the problems it creates. (Usually for new users, who are seen to be creating accidentally immense arrays.)
Now we can do things like this:
A = (0:4)' + (1:4)
A = 5×4
1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8
which is a true boon for those of us who desire an addition table. ;-)
Seriously the ability to expand singleton dimensions when performing operations like that makes our code often much simpler and far more easy to read. The bsxfun and repmat solutions always felt like just something we needed to accept, but they never felt like something truly pretty in my eyes.
Alessandro
Alessandro on 13 Mar 2024 (Edited on 13 Mar 2024)
I don't think I could express myself better than Petr Krysl in his reply on the blog post (link in the first answer). So I stop trying to explain why this is not an improvement after all (my opinion, of course).
But when you say "A difference is that some code which produced an error then...", well: errors are parts of life, and are a necessary step for learning. I teach a bit of MC coding in my class using MATLAB, and trust me: students may try to sum, subtract of multiplying things without having a clear idea of what they are actually doing. Now my students have more troubles in understaning their mistakes, just that. And I think also Mathworks has some problems in understanding its mistakes. Not sure about the dinosaurs.
John D'Errico
John D'Errico on 13 Mar 2024
"In my opinion this change of behaviour is unacceptable, because it breaks the compatibility."
I'm sorry, but no. Changes must happen over time in any language that has persisted for something like 40 years now. Sometimes changes must happen, and this change is one that was discussed at great depth.
Before bsxfun was introduced, there was a great deal of discussion on the topic. (I was involved in those discussions.) In fact, I even recall the idea of singleton expansion floated as an alternative, and at the time, it was voted down. I imagine after a few years, bsxfun was also found to have issues, and singleton expansion popped back to the top, as a better alternative.
The fact is, nobody will ever be happy with every change out there. As humans, we should be able to adapt to change.
Anyway, does this break compatibility? Arguably, NO. Valid code along these lines in a past release that did work pre-R2016b, will still work. A difference is that some code which produced an error then, now now longer produces an error. Such is life. This just means you need to update your own teaching to reflect a change that was made. Sorry. We must be able to adapt, or we all go the way of the dinosaurs.
Voss
Voss on 13 Mar 2024