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.
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!
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.
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.
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.
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:
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 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).