# Vector (transpose) Matrix addition - Why it could work despite different dimensions (Matlab)

I'm currently trying to learn Matlab so I was going through a tutorial that doesn't provide solutions to the exercises, and I thought the following two questions deserve some more attention in order to develop a deeper understanding of perhaps both the way the language works and Linear Algebra:

Given x = [6 1 -8], y = [1 1 2] and A = [-3 0 3; 6 -2 6], determine which of the following statements will correctly execute and provide the result. Try to understand why it fails.

(i) x + A

(ii) x' (which is x transpose) + y

x and y are row vectors, so in (ii) we're dealing with addition of vectors with dimensions 1x3 and 3x1 respectively, which I thought was impossible, and the resulting 3x3 matrix is also not intuitive to me at all.

For (i) I'm also lacking the mathematical intuition, but reading some Matlab documentation it seems that in vector matrix or matrix vector addition with different dimensions, Matlab treats the vector as a matrix with the same dimensions as the given matrix?

You are correct, mathematically speaking adding a $$1\times 3$$ vector to $$3\times 1$$ vector does not make much sense in terms of vector spaces, but it can be useful when doing calculations.

Matlab is doing something called "automatic broadcasting" which it adopted a few versions ago from Octave. Here you can read more about it:

https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/

What happens with broadcasting is the lacking dimensions are replicated:

$$[a,b,c] + \begin{bmatrix}x\\y\\z\end{bmatrix} \to \begin{bmatrix}a&b&c\\a&b&c\\a&b&c\end{bmatrix} + \begin{bmatrix}x&x&x\\y&y&y\\z&z&z\end{bmatrix}$$

So the row of the first vector gets copied as many times as needed to fit the 3 rows of the second vector and vice versa.

• Thanks so much!! This clarifies a whole lot :) – pink_paw_pads Sep 7 at 14:46