2
$\begingroup$

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?

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

$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks so much!! This clarifies a whole lot :) $\endgroup$ Commented Sep 7, 2019 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .