9.2. Array Indexing#

The elements of an array are indexed by their position starting at 1 for the first element. The index is written in rounded brackets following the array name, e.g., to access the element \(a_i\) of the 1D array a we use

a(i)

For two-dimensional arrays, the indices of an element are separated by a comma, e.g., to access the element \(a_{ij}\) of the 2D array A we use

array(i,j)

To index elements at the end of a row or column we can use the index end.

array(end)

Enter the following code into your program.

% Array indexing
fprintf("The first element in a is %d \n", a(1))
fprintf("The second element in a is %d \n", a(2))
fprintf("The last element in a is %d \n", a(end))
fprintf("The element in row 1 column 1 of A is %d \n", A(1,1))
fprintf("The element in row 2 column 1 of A is %d \n", A(2, 1))

Run your program and you should see the following added to the console.

The first element in a is 1 
The second element in a is 2 
The last element in a is 3 
The element in row 1 column 1 of A is 1 
The element in row 2 column 1 of A is 3 

9.2.1. Determining the size of an array#

The number of elements in a one-dimensional array can be determined using the length() function

length(array)

The number of rows and columns in a 2D array can be determined using the size() function

rows = size(array, 1)
cols = size(array, 2)

Enter the following code into your program.

% Array sizes
fprintf("The 1D array a has %d elements\n", length(a))
fprintf("The 2D array A has %d rows and %d columns\n", size(A, 1), size(A, 2))

Run your program and you should see the following added to the console.

The 1D array a has 3 elements
The 2D array A has 2 rows and 2 columns

9.2.2. Array slicing#

Sometimes we want to access multiple elements of an array at the same times. To do this we can use array slicing. This is done by specifying a range for the elements we want to access

array(start : stop : step]

start is the index of the first element in the range, stop is 1 more than the index of the last element in the range and step is the difference between the indices. If any these are unspecified Python uses default values of start = 0, stop = array length, step = 1.

To print the first 4 elements of b we use start = 1, stop = 4 and step = 1. Enter the following code into your program.

% 9.2.2 Array slicing
fprintf("\nThe first 4 elements of b are")
b(1:1:4)

Run your program and you should see the following added to the console.

The first 4 elements of b are
ans =

     1     2     3     4

Since MATLAB uses the default value step = 1 so we don’t need to specify the step value. Replace b(1:1:4) with b(1:4) and rerun your program, you should see that the output has not changed.

To print the last 5 elements of b we use start = 4 and stop = 9 (the length of the array). But since the default stop value is the length of the array we don’t need to specify this. Enter the following code into your program.

fprintf("The last 5 elements of b are")
b(5:end)

Run your program and you should see the following added to the console.

The last 5 elements of b are
ans =

     5     6     7     8     9

To print the middle 3 elements of b we use start = 4 (since array indexing starts at 0 this is the 4th element of b) and stop = 6 (as we want a step of 1 we don’t need to specify this). Enter the following code to your program.

fprintf("The middle three elements of b are")
b(4:6)

Run your program and you should see the following added to the console.

The middle three elements of b are
ans =

     4     5     6

To print all of the elements of b with an even number index we use start = 2, stop = 9 and step = 2. Enter the following code into your program.

fprintf("The elements of b with even indices are")
b(2:2:end)

Run your program and you should see the following added to the console.

The elements of b with even indices are
ans =

     2     4     6     8

To print all of the elements of b in reverse order we can use start = end, stop = 1 and step = -1. Enter the following code into your program.

fprintf("The elements of b in reverse order are")
b(end:-1:1)

Run your program and you should see the following added to the console.

The elements of b in reverse order are
ans =

     9     8     7     6     5     4     3     2     1

9.2.2.1. Slicing 2D arrays#

Slicing of 2D arrays is similar to that for 1D arrays with the exception that it needs to be done for both dimensions. Lets say we want to print the first row of the 2D array C. The first dimension specifies the row index we for us this is 1. The second dimension is the column index and since we want all elements in the row we have start = 1, stop = end and step = 1 which we can use default values for these which leaves a single :. Enter the following code into your program.

fprintf("The first row of A is")
A(1,:)

Run your program and you should see the following added to the console.

The first row of A is
ans =

     1     2

Lets do similar for the second column of A. Here we output all elements in the first dimension and use the column index 2. Enter the following code into your program.

fprintf("The second column of A is")
A(:,2)

Run your program and your should see the following added to the console output.

The second column of A is
ans =

     2
     4

9.2.3. Exercise#

Exercise 9.2

Using the arrays defined in Exercise 9.1 to print the following

  1. the third element of \(\vec{a}\)

  2. the element in row 1, column 2 of \(B\)

  3. the middle two elements of \(\vec{a}\)

  4. the third column of \(C\)

  5. the matrix formed by the last two rows and columns of \(D\)

  6. the matrix \(B\) with the rows in reverse order

  7. the first and third columns of \(D\)