Skip to main content

Section 10.1 Addition and Subtraction with Arrays

The addition and subtraction of arrays works in the exact way that you think that it would. When you attempt to use the \(+\) (addition) or \(-\) (subtraction) operator on arrays of identical size, the corresponding elements are added or subtracted. It is that easy.
In the general case, if we have a matrix A and a matrix B with elements:
\begin{equation*} [A]= \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \end{equation*}
\begin{equation*} [B]= \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix} \end{equation*}
(in this case \(a_{11}\) could be any number, \(a_{12}\) could be any number, etc)
Now if we subtract A from B we get:
\begin{equation*} [A]-[B]= \begin{bmatrix} a_{11}-b_{11} & a_{12}-b_{12} \\ a_{21}-b_{21} & a_{22}-b_{22} \end{bmatrix} \end{equation*}
We simply subtract the corresponding elements. Addition works the exact same way. It also works the exact same way with vectors (Recall, vectors are 1-dimensional arrays). The main thing to remember is that the arrays \([A]\) and \([B]\) must be the exact same size!
Try It!
To see it in action, open up MATLAB and define the following four arrays (if you do not remember how to create these arrays in MATLAB, see Chapter 9). You should notice that I am not showing you the exact commands to enter these variables into the MATLAB workspace. From now on, I will assume you know how to create vectors and matrices.
\begin{equation*} mat1= \begin{bmatrix} 4 & 12 \\ -5 & 2.6 \end{bmatrix} \end{equation*}
\begin{equation*} mat2= \begin{bmatrix} -10 & 2.3 \\ 5 & 7 \end{bmatrix} \end{equation*}
\begin{equation*} vec1= \begin{bmatrix} 0 & 12 & 24 \end{bmatrix} \end{equation*}
\begin{equation*} vec1= \begin{bmatrix} 11 &36 & 59 \end{bmatrix} \end{equation*}
Hint: the first matrix can be input by typing mat1=[4 12; -5 2.6]; but you need to figure out the rest on your own!

Example 10.1.1. Creating Matricies.

Here is an interactive code box that functions identically to the MATLAB command window.
Figure 10.1.2. Before continuing make sure your Workspace variables are identical!
Now that those four arrays are loaded into your Workspace (see figure 10.1.2 to the right to make sure you are on the same page) you can add or subtract the matrices. Keep these 4 arrays in your Workspace. We will be using them throughout the chapter.
First, try adding \([mat1]+[mat2]\) and storing that operation into a new variable called \([mat3]\text{.}\)

Example 10.1.3. Adding mat1 and mat2.

Here is an interactive code box that functions identically to the MATLAB command window.

Checkpoint 10.1.4. Values in mat3.

Notice that it simply adds up the corresponding elements. The 1st row, 1st column of \([mat3]\) contains the value \(-6\) which is just \(4+(-10)\) which are the values in the 1st row, 1st column of \([mat1]\) and \([mat2]\) respectively.
Using the variables stored in your workspace, try the following subtraction operation:
vec2-vec1
and store it in a new vector called vec 3.

Checkpoint 10.1.5. Accessing a vec3 Element.

Remember, arrays must be identical in size
Hopefully, you agree that addition and subtraction with arrays is pretty straightforward. If you still need a little practice, I suggest just trying out different combinations of vectors and matrices in MATLAB and adding and subtracting them until you can predict what the values of the output will be before you hit enter.
Just to make sure you understand when you do make mistakes, make sure that you still have the original 4 arrays in your Workspace, and try the following in the command window:
mat1+vec1

Example 10.1.6. Adding mat1 and vec1.

Here is an interactive code box that functions identically to the MATLAB command window.
You should see MATLAB displays an error Matrix dimensions must agree. The reason MATLAB is displaying this error to you is because the dimensions of mat1 and vec1 are NOT identical. Whenever you see the error Matrix dimensions must agree you should go back and look through your arrays to make sure they are the exact same size!
Adding or Subtracting a Scalar to an Array
Adding or subtracting a scalar from an array is even easier than adding or subtracting arrays. You should recall in Chapter 7 that we learned that scalars are just single numbers (as opposed to arrays which are collections of numbers).
In the general case if we want to add the number \(s\) to the array \([A]\text{:}\)
\begin{equation*} [A]+s= \begin{bmatrix} a_{11}+s & a_{12}+s \\ a_{21}+s & a_{22}+s \end{bmatrix} \end{equation*}
You simply add \(s\) to every value in the array \([A]\text{!}\) Subtraction works the exact same way.
Using the 4 variables still stored in your Workspace, subtract the scalar 4 from mat1 and store it in a new matrix called mat4.

Checkpoint 10.1.7. Subtracting a Scalar.

You have attempted of activities on this page.