Disable MATLAB's implicit expansion

白昼怎懂夜的黑 提交于 2019-12-19 08:32:54

问题


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.


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/49535504/disable-matlabs-implicit-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!