Disable implicit MATLAB extension

Recently, a function called MATLAB was added to R2016b, which causes a lot of headaches in the school I teach.

Nowadays, formulas that are traditionally considered illegal or at least shadow mathematicians are successfully executed:

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

Thus, adding a row vector to a column vector is considered as adding two matrices, which can be obtained from repeating vectors to β€œsuitable” sizes. In older versions, this would lead to an error message indicating that adding matrices with different sizes is not possible.

I think that asking why is a little wide, although if you know why, I would like to know. Instead, I will ask: is there a way to disable this functionality ? For novice programmers, this is a world of pain when ordinary math does not seem to be in the queue, and the resulting matrix often goes unnoticed, causing errors only later.

I do not see that this is a useful part of the syntax and behavior of MATLAB, since it requires too much interpretation, reading in the programmer's intentions. repmat exists for some reason, and a special function can be introduced to satisfy the need for this thing.

+2
source share
2 answers

This feature was introduced in Matlab R2016b. In older versions, this extension must be done either with repmat or with bsxfun . Newer versions have this implicit size extension to vectorize the calculation.

In this blog post, Steve Eddins of MathWorks says:

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

and it really makes sense in a computational context. I can say that for my purposes this implicit extension does things very often very often.

Of course, considering this from the point of view of algebra, it does not make sense. But if you think about it, most notes in computer language make no sense.

And since this is now part of the language, it should not be possible to disable this function (until Yair Altman tries to do this :P ).

+5
source

As @PhelypeOleinik mentioned, this (since R2016b) is the main part of the language and for good reason, as stated in the blog post that is being talked about.

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

  • Create a folder somewhere in your path called @double .
  • In this folder, create the plus.m file.

In the file, put something like the following:

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

If I have a comment above, you can put any code that you like: that may include code that checks the inputs for the "dimensional compatibility" rules, and errors if they don't match them.

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

PS, please, do not actually do this, the developers tried very hard to implement the implicit extension, and they will cry if they see that you disabled it like this ...

+4
source

Source: https://habr.com/ru/post/1264584/


All Articles