Strange results when adding two vectors

 Accepted Answer

Matt J
Matt J on 19 Mar 2018
It is the result of Implicit Expansion introduced in R2016b. What result were you expecting?

4 Comments

An error, as in my opinion one cannot add a vector of dimension [1x11] with a vector of dimension [11x1].
Can I somehow turn off this Implicit Expansion? It obviously contradicts linear algebra, in my opinion.
No, it cannot be turned off.
"It obviously contradicts linear algebra"
No, it does not. Linear algebra defines what happens when you use particular operators between arrays that have particular size relationships. Linear algebra does not define what happens when you use those operators between arrays that do not have those particular size relationships.
Remember that as far as linear algebra is concerned, adding a scalar to a vector is not defined behavior: linear algebra only defines adding between arrays of the same size. The operation
(1:10)-1
is not defined in linear algebra, which would require instead
(1:10)-1*ones(1,10)
So if you want to complain that MATLAB has defined a meaning for adding two vectors of different sizes, then you also need to advocate that (1:10)-1 should fail.
I do agree that it would be useful sometimes to off implicit expansion for debugging purposes -- but that is not the same as saying that MATLAB is wrong for defining a meaning for the operation.
Matt J
Matt J on 20 Mar 2018
Edited: Matt J on 20 Mar 2018
Can I somehow turn off this Implicit Expansion? It obviously contradicts linear algebra, in my opinion.
Like Walter and John, I don't agree that MATLAB has to disallow everything that linear algebra leaves undefined, but you can find further discussion of deactivating implicit expansion in my post here. I no longer advocate a deactivation switch, but I do think extending DBSTOP to intercept it could be useful.
Thank you all for your insight and for your time to answer my question! It has certainly cleared up a whole lot about this issue!
Thank you!

Sign in to comment.

More Answers (1)

Nope. Not insane. Just part of MATLAB, since the last few releases. Nor can you turn it off. If you don't like it, just don't add vectors that do not conform. You would have gotten an error in the past anyway.
Does it contradict linear algebra? Sorry, but not any more than other operations. MATLAB already did implicit expansion in many cases, in ways that were as arguably "contradictory". For example:
1 + rand(5,5)
So MATLAB has always expanded the scalar to be an appropriate size to conform to the array, adding 1 to every element.
As far as being contradictory to linear algebra, I have long argued that programming languages like MATLAB are not in fact mathematics. They are at best a simulation of mathematics, an approximation thereof. So while some aspects of MATLAB look like linear algebra, they are not even that. Were MATLAB truly mathematics, the following operation would return zero:
A = rand(1,10);
sum((A - 1).^2) - (sum(A.^2) - 2*sum(A) + length(A)*1)
ans =
-8.8818e-16
While you can decide you don't like implicit expansion, it is quite easy to get used to, and extremely nice at times. It can really clean up code, making it far more readable, and thus easier to debug and maintain. Conversely, the use of bsxfun in past releases was arguably the creator of some nasty looking code to do simple things.
I know, some people don't like it. Others do. That is life. Change happens, and we learn to live with it. Before long, you realize the change is not the work of the devil after all.
(Many years ago, in the days when I was paid to do less interesting stuff than I now do for free, our group was forced by IT edict to migrate to a different mainframe OS. The old one was just fine for what we did. Sniff. But within a few weeks, we realized the new system brought many advantages with its arrival.)

1 Comment

Thank you very much for your extensive answer! Very much appreciated, and in combination with the answers above it has certainly cleared up a lot!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!