Adding vectors of different sizes... works now? Can this be suppressed?

9 visualizaciones (últimos 30 días)
Jeff Glusman
Jeff Glusman el 12 de Feb. de 2025
Comentada: John D'Errico el 14 de Feb. de 2025
In previous versions of matlab, adding two vectors of different sizes would result in an error (this would align with rules of matrix addition).
For example:
[ 1 2 3 4] + [1 2 3 4]'
ans =
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
For example:
[ 1 2 3 4] + [1 2 3 4 5]'
ans =
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
Is there a way to suppress this new "feature"?
  6 comentarios
Star Strider
Star Strider el 13 de Feb. de 2025
It’s called ‘automatiic implicit expansion’ and appeared first in R2014b. Previously, it was necessary to use bsxfun to get that result.
John D'Errico
John D'Errico el 14 de Feb. de 2025
We had many lengthy discussions about BSXFUN, before it was introduced. At the time, implicit expansion was brought up as an alternative, but it never got any traction, AT THE TIME. I think they eventually realized that BSXFUN was nice, but not truly a good solution, and that implicit expansion was just better.
As @James Tursa said, I also truly love and appreciate this, and use it frequently.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Feb. de 2025
Editada: Walter Roberson el 12 de Feb. de 2025
There is no way to suppress implicit expansion.
Consider by the way that
3 + eye(4)
ans = 4×4
4 3 3 3 3 4 3 3 3 3 4 3 3 3 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
also breaks the rules of matrix addition, since size(3) is not the same as size(eye(4))
Likewise
3 * eye(4)
ans = 4×4
3 0 0 0 0 3 0 0 0 0 3 0 0 0 0 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
breaks the rules of matrix multiplication, since size(3,2) is not the same as size(eye(4),1) ["inner dimensions" do not match.]
  12 comentarios
Paul
Paul el 13 de Feb. de 2025
I see now in this comment that @Walter Roberson was referring to Matlab's implementation of mtimes, *. Don't know how I missed that on first read.
Walter Roberson
Walter Roberson el 13 de Feb. de 2025
Right. If we are going to insist that MATLAB obey the "rules" on matrix addition, then we need to insist that MATLAB obey the "rules" on matrix multiplication as well, by making it invalid to mtimes() a scalar by a matrix, and making it invalid to plus() a scalar and a matrix.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by