Section 4.1 Addition and Subtraction
Two vectors or two matrices can be added or subtracted in MATLAB just as they are added in Linear Algebra, using the simple
+
or -
operator.Vectors and matrices must be the same size (dimensions) in order for this to work:
If
\begin{equation*}
x = \begin{bmatrix}
x_1 & x_2 & x_3 & x_4
\end{bmatrix}
\end{equation*}
and
\begin{equation*}
y = \begin{bmatrix}
y_1 & y_2 & y_3 & y_4
\end{bmatrix}
\end{equation*}
then
\begin{equation*}
x + y =
\begin{bmatrix}
(x_1+y_1) & (x_2+y_2) & (x_3+y_3) & (x_4+y_4)
\end{bmatrix}
\end{equation*}
and
\begin{equation*}
x - y =
\begin{bmatrix}
(x_1-y_1) & (x_2-y_2) & (x_3-y_3) & (x_4-y_4)
\end{bmatrix}
\end{equation*}
Check out the following MATLAB example:
>> va = [8 5 3]; vb = [2 9 4]; >> vc = va - vb
vc = 6 -4 -1
The same syntax works to add and subtract matrices instead of just vectors:
If
\begin{equation*}
A = \begin{bmatrix}
a_{11} & a_{12} & a_{13}\\
a_{21} & a_{22} & a_{23}
\end{bmatrix}
\end{equation*}
and
\begin{equation*}
B = \begin{bmatrix}
b_{11} & b_{12} & b_{13}\\
b_{21} & b_{22} & b_{23}
\end{bmatrix}
\end{equation*}
then
\begin{equation*}
A + B =
\begin{bmatrix}
(a_{11}+b_{11}) & (a_{12}+b_{12}) & (a_{13}+b_{13})\\
(a_{21}+b_{21}) & (a_{22}+b_{22}) & (a_{23}+b_{23})
\end{bmatrix}
\end{equation*}
and
\begin{equation*}
A - B =
\begin{bmatrix}
(a_{11}-b_{11}) & (a_{12}-b_{12}) & (a_{13}-b_{13})\\
(a_{21}-b_{21}) & (a_{22}-b_{22}) & (a_{23}-b_{23})
\end{bmatrix}
\end{equation*}
Check out the following MATLAB example:
>> A = [8 5 3; 1 2 5]; B = [2 9 4; 6 3 2]; >> C = A + B
C = 10 14 7 7 5 7