7

Matlab R2016b comes with a̶ ̶n̶e̶w̶ ̶f̶e̶a̶t̶u̶r̶e̶ a monster that will kill us all: Operator implicit expansion.

Basically if you have a matrix A and a vector B, you can now just do A+B (without bsxfun or repmat). Sounds great right?

However if you provide a column vector and a row vector it also works!!

Example:

a = 1:4      % row vector
b = (1:4)'   % column vector

% before R2016b:    
a + b
   Matrix dimensions must agree.

% after R2016b:
a + b 
    ans =

    2     3     4     5
    3     4     5     6
    4     5     6     7
    5     6     7     8

Expands both vectors and gives a Matrix as result!! This is very bad, since you might be doing completely undesired operations without noticing.

So my question is: Is there a way of disabling implicit expansion and giving back a Matrix dimensions must agree error?

  • 4
    "This is very bad, since you might be doing completely undesired operations without noticing" ahhhh now there it comes, the aret of *good programming*! Yes, this is true, and it also happens in other languages (python's numpy had this long ago). The answer: We need to get used to it, and not rely in errors.. – Ander Biguri Feb 22 '17 at 15:52
  • 12
    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 Feb 22 '17 at 15:53
  • I understand expanding a vector to match a Matrix (which I agree is useful and great), but expanding two vectors into a matrix?? It doesn't even make mathematical sense. –  Feb 22 '17 at 16:00
  • 3
    You could probably do this by [overloading](http://stackoverflow.com/q/5365464/52738) [existing](http://stackoverflow.com/q/2425251/52738) [operators](http://stackoverflow.com/q/4239907/52738), but that will almost certainly cause you more harm than good. The MathWorks [isn't likely to provide any such option either](http://blogs.mathworks.com/loren/2016/11/10/more_thoughts_about_implicit_expansion/), so you should probably just bite the bullet and learn to explicitly check these things by default. – gnovice Feb 22 '17 at 16:01
  • 1
    @SembeiNorimaki None of the cases make mathematical sense. Its a programming trick. – Ander Biguri Feb 22 '17 at 16:10
  • 1
    oh, have they really copied broadcasting from GNU Octave? https://www.gnu.org/software/octave/doc/v4.2.0/Broadcasting.html – Andy Feb 22 '17 at 19:44
  • You say that "_if you have a matrix A and a vector B, you can now just do A+B_" sounds "_right_", whereas "_if you provide a column vector and a row vector it also works_" is "_a monster thant will kill us all_"? I don't get it. If you are fine with implicit expansion for matrices, why not for vectors? Vectors are matrices – Luis Mendo Feb 22 '17 at 19:51
  • 5
    It's a nice feature, but not allowing disabling it, is a **poor engineering decision**. In many cases I need my code to work with older Matlab versions, so I don't want to use that feature. On the other hand it allows bugs that are very difficult to find. – Rotem Feb 22 '17 at 21:35
  • 1
    Because if you have 2 vectors, in 90% of the cases you want to just add them, not expand both to create a matrix (that would be done in few cases for some special operations). In the other hand if you already have a matrix and a vector most probably you want to expand the vector to apply it to the matrix (eg: normalization of values...) –  Feb 23 '17 at 09:33
  • 1
    Implicit expansion as implemented is just such a bad idea. It would have been easy to do it with an operator, and Mathworks has never provided any kind of evidence for why that wouldn't have worked. It just sort of surprised the operation on us, not even adding it to the release notes at the time. There's too many ways for this to go wrong, but other commenters are right that there's almost no chance of Matlab being able to fix this mistake. It would break too much additional code. – Kenn Sebesta Oct 05 '17 at 20:16
  • Someone posted the same question today. I'm voting to close this one because the new one attracted two answers, and this one didn't. Not sure why, they're the exact same question. – Cris Luengo Mar 28 '18 at 20:48
  • 2
    Let me get this straight, you want code like `magic(3)-1` to throw errors too, or do you only want to disable it selectively when it suits you..? Because this is an example of implicit expansion that nobody seems to be bothered about... – Dev-iL Jun 12 '18 at 18:34

0 Answers0