Strange results when adding two vectors
Mostrar comentarios más antiguos
Can anyone explain the resulting output (see PDF)?
Thanks!
Respuesta aceptada
Más respuestas (1)
John D'Errico
el 19 de Mzo. de 2018
Editada: John D'Errico
el 19 de Mzo. de 2018
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 comentario
William Burton
el 20 de Mzo. de 2018
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!