Skip to main content

Section 9.4 Array and Matrix Addressing

Array Addressing
You might already be able to see why arrays are powerful tools to utilize in MATLAB. For example, we could have arrays that correspond to the position of a moving object with respect to time. Let’s pretend that we have the following position and time data. Go ahead and define the following variables in your workspace by typing them into the command window.

Example 9.4.1. Vector Addressing Practice.

Here is an interactive code box that functions identically to the MATLAB command window.
You might recall from physics that we can calculate the average velocity between two points in time using this data. We can see that the object starts at time 0 at position 0. It then moves in the positive direction 4 units in 5 time units. We could calculate the average velocity of the object for any time period by:
\(avgVelocity=\frac{x_2-x_1}{t_2-t_1}\)
where \(x_1\) and \(x_2\) correspond to postions and \(t_1\) and \(t_2\) correpsond to times.
Since we have already created the arrays time and position in MATLAB we can use array addressing to pull out individual numbers within the array.
Vector Addressing
Figure 9.4.2. Example of our position array.
In this example, our array position has 5 elements in it corresponding to the numbers 0, 4, 8, 12, and 30. In this example, the number 8 has an address of 3. It is the third element in the array position. To access this number, we can call the array position and specify that we want the third element out of it.
Try typing this into the command window:
position(3)
You should notice that MATLAB stores the number 8 into the ans variable. We have accessed the number 8 from the position array! It may not be obvious now (some of you may be asking, why not just type in the number 8?) but believe me when I say this is a fundamental skill to have in MATLAB and is critical to your future success!
See if you can guess what the output of the following command will be, before typing it in:
anyone_home = position(4) + 4
What happens when you type that in? You create a new variable called anyone_home and it stores the number 16 in it. Why 16? Because position(4) accesses the 4th element in the array position!

Checkpoint 9.4.3. Calculating Velocity from Position Data.

Which of the following lines of code would correctly calculate the velocity of the object from time 15 to time 20? (Hint: the answer should be 3.6)
  • vel = (position(5)-position(4))/(time(5)-time(4))
  • Correct. This uses the proper forward difference between the position and time values.
  • vel = (position(4)-position(5))/(time(5)-time(4))
  • Incorrect. The sign of the numerator is reversed, which flips the direction of velocity.
  • vel = (position(5)-position(4))/(time(4)-time(5))
  • Incorrect. The time difference has the wrong sign, which produces an incorrect velocity.
  • vel = (position(4)-position(5))/(time(4)-time(5))
  • Correct. Both numerator and denominator are reversed, which cancels the sign change and gives the correct velocity.
As you can see, addressing values inside of vectors is as easy as typing in the vector name, followed by parenthesis, and then typing in the number that corresponds to its position!
Try It
Brain workout time. Try creating different size row and column vectors with random numbers and use array addressing to make sure that you understand how to pull out the correct values. Array addressing on row and column vectors works the exact same way. The addresses start at 1 and go from there.

Example 9.4.4. Array Practice.

Here is an interactive code box that functions identically to the MATLAB command window.
Matrix Addressing
Matrix addressing works the same way as vector addressing, the only difference is that with matrices we need to specify both the row and the column of the value of interest. Take a look at figure 9.4.5 for an example.
Figure 9.4.5. Example of addressing the number 14 in the matrix matrix.
Go ahead and enter the matrix in figure 9.4.5 into your workspace. It is good practice to remember how to enter all of the values. When you are done double-check that your matrix is the same as mine.
Now that you have done that, let’s try accessing the value 18. We can see that it is in the 5th row and the 2nd column of the matrix. That means, to get the number 18 we can type in:
matrix(5,2)
And now you know array addressing for both matrices and vectors! The main trick to remember is that when we are talking about array addressing (and honestly pretty much everything dealing with a matrix in MATLAB) it always goes ROWS then COLUMNS.
You know the drill. Brain workout time. I suggest that you make a random 4x5 matrix on a sheet of paper. Make sure that you can successfully type that matrix into MATLAB. Next, use array addressing to make sure that you can pull out numbers successfully. The nice thing about not using a square matrix is, that it will show you if you really understand the row/column law of MATLAB.

Checkpoint 9.4.6. Evaluating a Matrix Expression.

Given a matrix variable stored in your workspace, what is the value of matrix(2,2) + matrix(4,1) - matrix(1,1)?
  • 17
  • Incorrect. This does not match the correct substitution from the matrix entries.
  • -4
  • Incorrect. Check the indexing and sign carefully when substituting values.
  • -2
  • Correct. Substituting the correct matrix entries yields -2.
  • 10
  • Incorrect. This value may come from a different interpretation of the indices.
You have attempted of activities on this page.