In today’s lesson, we will cover:
1. Matrix Indexing
2. Colon Operator
3. Linear Spacing
4. Logarithmic Spacing
VIDEO COMING SOON!
Matrix Indexing
Indexing is when we access a value based on its location in an array (such as the 3rd row, or the element in the 2nd row and 3rd column). To do this, we should be familiar with the term indices. This is the plural of the word index. There are two main types of indexing:
(a) Indexing with Element Positions
In matrix indexing, we are usually referring to one element within the matrix – one specific row and column. We have two indices in this case – the first index is the row and the second index is the column of the matrix, otherwise denoted as i and j. Selecting an element out of matrix A for example, will be denoted as below. If we have a matrix A, then indices i and j will refer to the number row and column that we are indexing:
Let’s take a look at the example below. Here I have asked MATLAB to fetch the 3rd row, 2nd column element – which was 2. This is because I had typed A(3,2). The ‘A’ indicated it was from matrix A. Then the 3 indicated 3rd row, and 2 indicated 2nd column.
If instead you want to fetch an entire row or column, enter the number of that row or column in the appropriate spot within the code. In the other spot, use a colon.
So for example, if I wanted to fetch the 2nd row out of matrix A, I would use A(2,:). I would put 2 in the position where the row number goes, and a colon in the position where the column number would go. This is because I want to select all columns in the 2nd row, which ends up giving me the entire 2nd row.
This is what I’ve done in the example here:
To change a number within an array, we need to use indexing to replace that value. In this example, I used indexing to get MATLAB to show the 2nd row, 1st column of matrix A – it’s 1.
But what if I wanted to change it to a different value – say, 5? In that case, I use the same syntax, but set it equal to 5, like I’ve done below using A(2,1)=5 … then MATLAB will output a new matrix. This will be the same as matrix A, but with the number in the 2nd row and 1st column as 5 instead of what it previously was.
Here’s another example of how to use indexing to replace values in an array. Say we have the following matrix A:
If I wanted to replace the value 3 (in the 3rd column, 1st row) with the number 1, I could use indexing with that row and column number to change it:
This is the output, with the specified value now changed to a 1:
What if we wanted to remove a number? In a matrix, you could either set a number to 0, or just remove an entire row or column. You can’t remove one value, because then the matrix won’t be rectangular. To delete one row or column though, you can use the colon operator to specify which rows and columns you want to keep, and the rest will be removed. Say we have the same matrix A as the example above, and we only want to keep rows 1 and 3, with all the columns. We would use the syntax array([rows to keep], [columns to keep]).
In this case, the array (matrix) is variable A, the rows to keep are 1 and 3, and the columns include all of them. This is why I have used A([1,3],:). The colon operator in the place of the columns indicates we want to keep all the columns, and the [1,3] in place of the rows indicate we want to keep the 1st and 3rd rows. The new matrix that was created now has row 2 removed, and includes only rows 1 and 3:
Now although you can’t remove just one number from a matrix, you could remove a number if you have a vector. To do this, you will indicate that the position is empty, using empty square brackets. Here we have vector A:
If I want to remove the 5th number from the vector (number 5), I would use indexing to set it equal to an empty bracket. This is why I have used A(5) = [ ], because I am using vector A, specifying that I would like to use the 5th element, and then I set it equal to the empty brackets [ ] to remove that value from the vector.
(b) Indexing with a Single Index
Another type of indexing is where we don’t have to specify a certain row or column, but rather we take the fact that MATLAB stores matrices as one large column vector, and using indexing accordingly.
I have the following matrix A:
Now I want to extract the value in the 2nd row, 3rd column. Instead of using A(2,3), this value is the 8th value stored in the column vector of A. How do I know this? Well, let’s make MATLAB put this matrix into the column form and see for ourselves using A(:). See how the value in the 2nd row, 3rd column – which was 6 – is the 8th value in the column vector?
Now that we know this, we can use A(8) to call that value – and it’s 6, as you can see in the output below.
Colon Operator
The colon operator is used in many different ways in MATLAB. It can either be used to:
(a) Select a row or column of an array
(b) Create a spaced vector
Let’s take a look at these a bit more in-depth:
(a) The first example of where the Colon Operator is used is when selecting/deleting a row or column
To select certain rows or columns and delete the others, we need to use the syntax A(row, column), where you will replace A with the variable of the array, and the words row and column can be replaced with a couple different options:
(a) A colon (to include all rows or columns)
(b) A number (to select a specific row or column)
(c) A set of numbers (to include multiple rows or columns). In this case, use the empty vector operator, [ ] to specify what elements of the matrix you want to keep. This will delete the other elements within the matrix, and keep the ones you selected. The empty vector operator is used here when you want to select multiple rows or columns, but not all of them
Let’s take a look at what this means and how to use the empty vector operator, as well as the other options listed above. I have created a matrix A, which has 3 rows and 3 columns (a 3×3 matrix). I have done some examples below, highlighted in red.
In the first example above, I have done A(3,:) which selects the third row and all columns of matrix A, and gives this as the output.
In the second example above, I have reversed the places of the 3 and the colon, to show that only the 3rd column gets output instead of the 3rd row. Both examples 1 and 2 use a number as well as a colon in the syntax (highlighted in red) for selecting the appropriate elements within the matrix.
In the third example above, I have selected all the rows, but only the 2nd and 3rd columns. To select multiple columns (in my case, the 2nd and 3rd) I had to use square brackets with the numbers of the columns I wanted to keep (eg. [2 3] like I had).
Here is when you can use the colon operator for selecting different sections of an array, in a summed up picture:
(b) The second example where the Colon Operator is used is when creating a spaced vector
Sometimes, we will encounter very large arrays where we don’t want to manually type out all the numbers. If they are all evenly spaced, the colon operator can be used. For example, say you would like to count from 0 to 5 in steps of 0.1. The syntax would be first number:step:last number. Replacing the first and last numbers in the array, as well as the step we want to count up by, we get 0:0.1:5. Putting this into MATLAB gives the answer shown below, reading the array side to side. This was a lot easier than typing out each element individually!:
Linear Spacing
There is a second way to create arrays automatically rather than manually typing them. We can use linear spacing, which is similar to the colon operator. The difference is that rather than specifying what number you’d like to count up by, you can (but don’t have to) to enter how many data points you want in between the first and last numbers. The syntax for this is either linspace(a,b), where you can replace a and b by the first and last number in the array, or linspace(a,b,n), which has the added n to specify the number of data points you want. If you don’t specify how many points you’d like, the values will, by default, be linearly spaced.
Take a look at this example – linspace(0,10,5) generated a vector that went from 0 to 10, with 5 numbers in the vector.
Logarithmic Spacing
coming soon