6

Recently, in R2016b a feature was added to MATLAB, which is causing a lot of headaches in the school where I teach.

Nowadays formulae, which traditionally would be considered illegal or at least shady maths are executed successfully:

[1, 2] + [3, 4]'    -> [4, 5; 5, 6]
[1, 2]' + [3, 4, 5] -> [4, 5, 6; 5, 6, 7]

So adding a row vector to a column vector is treated as an addition of two matrices one can get from repeating the vectors up to the "suitable" dimensions. In older versions this would have produced an error message informing that the addition of matrices with different dimensions is not possible.

I think asking why is a bit broad, although if you do know why, I'd love to know. Instead I will ask, is there a way to disable this functionality? To novice programmers this is a world of hurt when the conventional mathematics doesn't seem to be in line and the resulting matrix often goes unnoticed causing errors only later on.

I can not see this being a useful part of MATLAB syntax and behavior, as it requires too much interpretation, reading into the intention of the programmer. repmat is there for a reason, and a dedicated function could be introduced to accommodate for the need of this thing.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Felix
  • 2,548
  • 19
  • 48
  • 2
    It's called *implicit expansion*, or *broadcasting*, which previously was covered by [`bsxfun`](http://mathworks.com/help/matlab/ref/bsxfun.html). It's actually very useful in trying to vectorise operations, see [this question on that](https://stackoverflow.com/q/29719674/5211833). As to the *why*, you'll have to ask The MathWorks, as to how to turn it of: I wouldn't know, except by downgrading to R2016a or earlier – Adriaan Mar 28 '18 at 13:18
  • 1
    While I understand the sentiment, this feature persistently requested from mathworks over a decade while other languages implemented very early. So they are even late. You cannot expect the coding practice to be consistent with math. If you explain broadcasting there will be no shady operations instead of forbidding the behavior of the software. – percusse Mar 28 '18 at 13:18
  • @Adriaan Fair enough, I see! – Felix Mar 28 '18 at 13:21
  • 2
    @percusse Yes, but apparently this functionality _was_ provided with `bsxfun`. And Matlab is otherwise quite consistent with math when it comes to matrix operations. I stand by my argument, that the engine should not interpret what the programmer ment, but execute exactly what was written. Perhaps that's a bit rigid for some. – Felix Mar 28 '18 at 13:25
  • 3
    I'll reiterate [my previous comment](https://stackoverflow.com/questions/42396134/disable-matlab-r2016b-implicit-expansion#comment71939612_42396134): It cannot be disabled. If array size and shape are critical to your application then you should be checking them. Relying on errors to validate your data is not a robust solution in any language. – sco1 Mar 28 '18 at 13:47
  • @excaza Mm yes, given you are working with seasoned professionals. In an introductory course to numerical methods this might not be the best thing to focus on while the students are still learning Matlab. But I appreciate the view. – Felix Mar 28 '18 at 13:50
  • 2
    That doesn't make any sense. An introductory course is *exactly* where you want people to learn how to properly validate their data. – sco1 Mar 28 '18 at 13:52
  • 1
    @excaza I prefer getting an error message and developing a habit of using e.g. column vectors with intent to chasing vague misbehavior in the program to eventually find that one vector was suddenly a matrix.. It'd be most tedious to check the dimensions of operands constantly, even though maybe used in larger applications. But that's just me. – Felix Mar 28 '18 at 14:04
  • 1
    @Felix perhaps.... you are wrong ? – Ander Biguri Mar 28 '18 at 14:20
  • 5
    @AnderBiguri Would not be the first time, and I'll surely continue making mistakes. Hopefully I'll learn to make less. – Felix Mar 28 '18 at 15:08
  • @Felix That is a good attitude to have ;) – Ander Biguri Mar 28 '18 at 15:09
  • 2
    Note that MATLAB has always done implicit singleton expansion for scalars: `M+v`, with `v` a scalar, is not at all valid linear algebra. Now they have finally extended this functionality to any singleton dimension. It's awesome! I'm sorry it confuses your students, though. They'd be confused with the same behavior in Octave, NumPy and just about any other numerical package out there. If you want MATLAB to only do linear algebra, you'll have to look for a version of MATLAB from 1986 or so. :) – Cris Luengo Mar 28 '18 at 20:07
  • @CrisLuengo That's a good point. – Felix Mar 28 '18 at 20:23
  • 1
    Interestingly in [julia](https://julialang.org) broadcasting/expansion is enabled by placing a dot before an operator. – rahnema1 Mar 29 '18 at 06:27
  • It's not just novice programmers, it's programmers of all experience levels. Implicit expansion, as implemented, breaks linear algebra rules. It could have been accomplished without overloading the linalg operators, for instance `@+`, `@-`, etc... – Kenn Sebesta Oct 09 '20 at 02:12
  • @KennSebesta I'll admit it now, I think broadcasting is an important feature. When you are aware of it, it makes so many operations a lot easier. A bit of confusion at the start but well worth it. – Felix Oct 09 '20 at 05:31
  • I think the feature is an excellent one, my beef is the syntax. And personally, I don't think it's worth it because I rarely need the feature but I always want the system to fail when I ask it to do an illegal (in the sense of linear algebra rules) operation. I find the operator overloading so dangerous it pushed me to use Julia. – Kenn Sebesta Oct 09 '20 at 11:09

2 Answers2

7

As mentioned by @PhelypeOleinik, this is (since R2016b) a core part of the language, and for good reasons, as detailed in the blog post referred to.

However, if you REALLY want to disable it...

  1. Make a folder somewhere on your path, called @double.
  2. In this folder, make a file plus.m.

In the file, put something like the following:

function out = plus(in1, in2)
    % do some things here
    out = builtin('plus', in1, in2)

Where I have a comment above, you can put whatever code you like: which could include code that checks the inputs for the "size-compatibility" rules you want, and errors if it doesn't meet them.

Do something similar for the functions minus, times, ldivide, rdivide, power, and other functions you want to modify.

PS please don't actually do this, the developers worked very hard to implement implicit expansion, and they'll cry if they see you disabling it like this...

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 2
    Thanks for the method, but coming at new studens with "Before we get to actual Matlab, let's first override the basic addition operator implicit expansion properties via this code snippet!" would be less than ideal. – Felix Mar 28 '18 at 16:49
5

This feature was introduced in Matlab R2016b. In older versions, this expansion had to be done either with repmat or with bsxfun. Newer versions feature this implicit expansion of dimensions to vectorize the calculation.

In this blog post Steve Eddins, from MathWorks says that:

Other people thought that the new operator behavior was not sufficiently based on linear algebra notation. However, instead of thinking of MATLAB as a purely linear algebra notation, it is more accurate to think of MATLAB as being a matrix and array computation notation.

and it really does make sense in a computational context. I can say that for my uses, this implicit expansion does make things easier very often.

Of course, seeing this from the point of view of algebra, it doesn't make sense. But if you think about it, most computer language notation wouldn't make sense.

And since this is now part of the language, it shouldn't be possible to disable the feature (until Yair Altman tries to do so :P).

Phelype Oleinik
  • 671
  • 8
  • 22