2.3. Array Indexing#
The individual elements of an array are accessed using an index that gives its position in the array. The first element has the index 0, the second element as the index 1, and so on. The index is written in square 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
A[i,j]
To index the last element of a row or column we can use the index -1
.
a[-1]
The penultimate element is indexed using -2
and so on. Enter the following code into your program.
# Array indexing
print(f"The first element in a is {a[0]}")
print(f"The second element in a is {a[1]}")
print(f"The last element in a is {a[-1]}")
print(f"The element in row 1 column 1 of A is {A[0,0]}")
print(f"The element in row 2 column 1 of A is {A[1,0]}")
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
2.3.1. Determining the size of an array#
The number of elements in a one-dimensional array can be determined using
len(array)
The number of rows and columns in a 2D NumPy array can be determined using the .shape()
property.
array.shape
This returns the tuple (rows, columns)
. So the number of rows and columns in an array can be determined using array.shape[0]
and array.shape[1]
respectively. Enter the following code into your program.
print()
print(f"The 1D array a has {len(a)} elements")
print(f"The 2D array A has {A.shape[0]} rows and {A.shape[1]} columns \n")
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
2.3.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 = 0
, stop = 4
and step = 1
. Enter the following code into your program.
print(f"The first 4 elements of b are {b[0:4:1]}")
Run your program and you should see the following added to the console.
The first 4 elements of b are [1 2 3 4]
Since Python uses default values of start = 0
and step = 1
, if we want to return the first 4 elements we don’t need to specify the start
or step
values. Replace b[0:4:1]
with b[: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.
print(f"The last 5 elements of b are {b[4:]}")
Run your program and you should see the following added to the console.
The last 5 elements of b are [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
), stop = 6
and since we want a step of 1 we don’t need to specify the step
. Enter the following code to your program.
print(f"The middle three elements of b are {b[3:6]}")
Run your program and you should see the following added to the console.
The middle three elements of b are [4 5 6].
To print all of the elements of b
with an even number index we use start = 0
, stop = 9
and step = 2
. Enter the following code into your program.
print(f"The elements of b with even indices are {b[0:9:2]}")
Run your program and you should see the following added to the console.
The elements of b with even indices are [1 3 5 7 9]
Again, we can make use of the default start
and stop
values replace b[0:9:2]
with b[::2]
. Rerun your program and you should see that the output has not changed.
To print all of the elements of b
in reverse order we can use step = -1
. Enter the following code into your program.
print(f"The elements of b in reverse order are {b[::-1]}")
Run your program and you should see the following added to the console.
The elements of b in reverse order are [9 8 7 6 5 4 3 2 1]
Note
Indexing an array can become quite confusing. Don’t worry if you don’t fully understand it at first. In practice even experienced programmers test the indexing using a print()
function to make sure they are accessing the correct elements of an array.
2.3.3. 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 A
. The first dimension specifies the row index we for us this is 0. The second dimension is the column index and since we want all elements in the row we have start = 0
, stop = 4
and step = 1
which we can use default values for these which leaves a single :
. Enter the following code into your program.
print()
print(f"The first row of A is {A[0,:]}")
Run your program and you should see the following added to the console.
The first row of A is [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 1. Enter the following code into your program.
print(f"The second column of A is {A[:,1]} \n")
Run your program and your should see the following added to the console output.
The second column of A is [2 4]
Note how the elements of the second column of A
have been printed as a row vector. This is because Python prints all 1D arrays as a row array.
2.3.4. Exercise#
Using the arrays defined in Exercise 2.1 to print the following
the third element of \(\vec{a}\)
the element in row 1, column 2 of \(B\)
the middle two elements of \(\vec{a}\)
the third column of \(C\)
the matrix formed by the last two rows and columns of \(D\)
the matrix \(B\) with the rows in reverse order
the first and third columns of \(D\)