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

MATLABmatricestransposevectors

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 1×3 and 3×1 respectively, which I thought was impossible, and the resulting 3×3 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?

Any thoughts/ideas/suggested readings are appreciated! Thank you for your patience.

Best Answer

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.


Edit: Mathematically speaking, what actually happens is that instead of calculating $${\bf v}^T+\bf w$$ The software finds $N,M\in \mathbb Z$ so that the following expression makes sense:

$$({\bf v}^T \otimes {\bf 1}_N) + ({{\bf 1}_M}^T\otimes \bf w)$$

where $\otimes$ is a Kronecker product and then calculates it.