Arrays
Overview
Teaching: 15 min
Exercises: 5 minQuestions
How can I access subsets of data?
Objectives
Select individual values and subsections from data.
Array indexing
Let’s create an 8-by-8 “magic” Matrix:
>> M = magic(8)
ans =
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
We want to access a single value from the matrix:
To do that, we must provide its index in parentheses:
>> M(5, 6)
ans = 38
Indices are provided as (row, column). So the index (5, 6)
selects the element
on the fifth row and sixth column.
An index like (5, 6)
selects a single element of
an array, but we can also access sections of the matrix, or slices.
To access a row of values:
we can do:
>> M(5, :)
ans =
32 34 35 29 28 38 39 25
Providing :
as the index for a dimension selects all elements
along that dimension.
So, the index (5, :)
selects
the elements on row 5
, and all columns—effectively, the entire row.
We can also
select multiple rows,
>> M(1:4, :)
ans =
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
and columns:
>> M(:, 6:end)
ans =
6 7 57
51 50 16
43 42 24
30 31 33
38 39 25
19 18 48
11 10 56
62 63 1
To select a submatrix,
we have to take slices in both dimensions:
>> M(4:6, 5:7)
ans =
36 30 31
28 38 39
45 19 18
We don’t have to take all the values in the slice—if we provide
a stride. Let’s say we want to start with row 2
,
and subsequently select every third row:
>> M(2:3:end, :)
ans =
9 55 54 12 13 51 50 16
32 34 35 29 28 38 39 25
8 58 59 5 4 62 63 1
And we can also select values in a “checkerboard”,
by taking appropriate strides in both dimensions:
>> M(1:3:end, 2:2:end)
ans =
2 61 6 57
26 37 30 33
15 52 11 56
Slicing
A subsection of an array is called a slice. We can take slices of character strings as well:
>> element = 'oxygen'; >> disp(['first three characters: ', element(1:3)]) >> disp(['last three characters: ', element(4:6)])
first three characters: oxy last three characters: gen
What is the value of
element(4:end)
? What aboutelement(1:2:end)
? Orelement(2:end - 1)
?For any size array, MATLAB allows us to index with a single colon operator (
:
). This can have surprising effects. For instance, compareelement
withelement(:)
. What issize(element)
versussize(element(:))
? Finally, try using the single colon on the matrixM
above:M(:)
. What seems to be happening when we use the single colon operator for slicing?Solution
Exercises using slicing
element(4:end) % Select all elements from 4th to last ans = 'gen' element(1:2:end) % Select every other element starting at first ans = 'oye element(2:end-1) % Select elements starting with 2nd, until last-but-one ans = 'xyge'
The colon operator ‘flattens’ a vector or matrix into a column vector. The order of the elements in the resulting vector comes from appending each column of the original array in turn. Have a look at the order of the values in
M(:)
vsM
Key Points
M(row, column)
indices are used to select data points
:
is used to take slices of data