Skip to main content

Section 9.6 Advanced Array Addressing with the : Operator

You can couple what we have learned about the : operator with addressing for arrays to do some really cool stuff. The key thing to remember is that the : operator defaults to a spacing of 1! That means you can use the : operator to address a range of elements in a vector or a matrix.
Using : with vectors
As mentioned above, we can use the : operator to address a range of elements from a vector. The most general use case is as follows:
range_from_vector = vector_name(m:n)
This would take out the \(m^{th}\) through the \(n^{th}\) elements of vector vector_name and store them in a new vector called range_from_vector. It’s that easy!
For this example, let’s add a random vector v_man to our workspace as follows:
v_man = [22 61 45 51 68 98 11 90 17]
We can use the : operator to address different ranges of elements from the vector v_man. Let’s say for this application we want to create a new variable called two_to_five and we want to store the 2nd through the 5th elements of vector v_man.
two_to_five = v_man(2:5)
That will accomplish our goal!
Before you try that on your computer, do you know what those elements’ values should be? Take a second to write them down before trying to make sure you understand array addressing!

Example 9.6.1. : with Vector Practice.

Here is an interactive code box that functions identically to the MATLAB command window.
Using : with Matricies
Because matrices have two dimensions, there are many more ways that we can use the : operator to address ranges of elements within a matrix. However, the logic is the exact same as with vector addressing. Again, the key is to remember the row/column law of MATLAB.
Hopefully, you still have some matrices stored in your workspace. Before you continue, see if you can guess how to use the : s operator on matrices to pull out a range of values. Can you figure out at least a couple of ways to make it work?
If you couldn’t figure any out, that is OK! The fact that you tried is all that matters.
Here are some of the ways that you can address a range of elements within a matrix. For this example, let’s consider the general case of a matrix A. It can have any number of rows and columns.
For your brain workout for this section, try every one of the bullet points above on one of the matrices we have previously defined to make sure you understand how this works!

Example 9.6.2. : with Matrix Practice.

Here is an interactive code box that functions identically to the MATLAB command window.
Housekeeping
The last thing I would like to mention about scripts is that it is a good idea to include what I call β€œhousekeeping” lines of code at the top of your script. Most of my scripts start with clear and clc. For example, look at a script that we will work on in Chapter 12 below in figure 9.6.3.
Figure 9.6.3. An example script.
Don’t worry about what this script does (yet). Instead, notice how I started the script with a clc and then a clear command. This is good practice to ensure that your scripts will work on a fresh start of MATLAB and do not rely on a variable that is currently in the workspace. You are not required to start your scripts this way, but it is good practice.
You have attempted of activities on this page.