Arrays
Last updated on 2024-03-22 | Edit this page
Estimated time: 40 minutes
Overview
Questions
- How can I access the information in an array?
Objectives
- Learn how to create multidimensional arrays
- Select individual values and subsections of an array.
Initializing an Array
We just talked about how MATLAB thinks in arrays, and
declared some very simple arrays using square brackets. In some cases,
we will want to create space to save data, but not save anything just
yet. One way of doing so is with zeros
. The function zeros
takes the dimensions of our array as arguments, and populates it with
zeros. For example,
OUTPUT
Z =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
creates a matrix of 3 rows and 5 columns, filled with zeros. If we had only passed one dimension, MATLAB assumes you want a square matrix, so
OUTPUT
Z =
0 0 0
0 0 0
0 0 0
yields a 3×3 array. If we want a single row and 5 columns, we need to
remember that MATLAB reads rows
×columns
,
so
OUTPUT
Z =
0 0 0 0 0
This way zeros
function works is shared with many other
functions that create arrays.
For example, the ones
function is nearly identical, but the arrays are filled with ones,
and the rand
function assigns uniformly distributed random numbers between zero
and 1 to each space in the array.
Callout
Note: This is when supressing the output becomes
more important. You can more comfortably explore the variables
R
and O
by double clicking them in the
workspace.
The ones
function can actually help us initialize a
matrix to any value, because we can multiply a matrix by a constant and
it will multiply each element. So for example,
Produces a 3×6 matrix full of fives.
The magic
function works in a similar way, but you can only declare square
matrices with it. The magic thing about them is that the sum of the
elements on each row or column is the same number.
OUTPUT
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
In this case, each row or column adds up to 34. But how could I tell in a bigger matrix? How can I select some of the elements of the array and sum them, for example?
Array indexing
Array indexing, is the method by which we can select one or more different elements of an array. A solid understanding of array indexing will be essential to working with arrays. Lets start with selecting one element.
First, we will create an 8×8 “magic” matrix:
OUTPUT
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. In a 2D array, this means the row and column of the element
separated by a comma, that is, as (row, column)
. This index
goes after the name of our array. In our case, this is:
OUTPUT
ans = 38
So the index (5, 6)
selects the element on the fifth row
and sixth column of M
.
Callout
Note: Matlab starts counting indices at 1, not 0! Many other programming languages start counting indices at 0, so be careful!.
An index like the one we used selects a single element of an array, but we can also select a group of elements if instead of a number we give arrays of indices. For example, if we want to select this submatrix:
we want rows 4, 5 and 6, and columns 5, 6 and 7, that is, the arrays
[4,5,6]
for rows, and [5,6,7]
for columns:
OUTPUT
ans =
36 30 31
28 38 39
45 19 18
The :
operator
In matlab, the symbol :
(colon
)
is used to specify a range. The range is specified as
start:end
. For example, if we type 1:6
it
generates an array of consecutive numbers from 1 to 6:
OUTPUT
ans =
1 2 3 4 5 6
We can also specify an increment other than one. To specify
the increment, we write the range as start:increment:end
.
For example, if we type 1:3:15
it generates an array
starting with 1, then 1+3, then 1+2*3, and so on, until it reaches 15
(or as close as it can get to 15 without going past it):
OUTPUT
ans =
1 4 7 10 13
The array stopped at 13 because 13+3=16, which is over 15.
The rows and columns we just selected could have been specified as
ranges. So if we want the rows from 4 to 6 and columns from 5 to 7, we
can specify the ranges as 4:6
and 5:7
. On top
of being a much quicker and neater way to get the rows and columns,
MATLAB knows that the range will produce an array, so we do not even
need the square brackets anymore. So the command above becomes:
OUTPUT
ans =
36 30 31
28 38 39
45 19 18
Checkerboard
Select the elements highlighted on the image:
Selecting whole rows or columns
If we want a whole row, for example:
we could in principle pick the 5th row and for the columns use the
range 1:8
.
OUTPUT
ans =
32 34 35 29 28 38 39 25
However, we need to know that there are 8 columns, which is not very robust.
The key-word end
When indexing the elements of an array, the key word end
can be used to get the last index available.
For example, M(2, end)
returns the last element of the
second row:
OUTPUT
ans =
16
We can also use it in combination with the :
operator.
For example, M(5:end, 3)
returns the elements of column 3
from row 5 until the end:
OUTPUT
ans =
35
22
14
59
We can then use the keyword end
instead of the 8 to get
the whole row with 1:end
.
OUTPUT
ans =
32 34 35 29 28 38 39 25
This is much better, now this works for any size of matrix, and we don’t need to know the size.
As you can see, the :
operator is quite important when
accessing arrays!
We can use it to select multiple rows,
OUTPUT
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
or multiple columns:
OUTPUT
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
or even the whole matrix. Try for example:
and you’ll see that it returns all the elements of M. The result,
however, is a column vector, not a matrix. We can make sure that the
result of M(:)
has 8x8=64 elements by using the function size,
which returns the dimensions of the array given as an input:
OUTPUT
ans =
64 1
So it has 64 rows and 1 column. Effectively, then, M(:)
‘flattens’ the array into a column vector. The order of the elements in
the resulting vector comes from appending each column of the original
array in turn. This is the result of something called linear
indexing, which is a way of accessing elements of an array by a
single index.
Master indexing
Select the elements highlighted on the image without using the
numbers 5 or 8, and using end
only once:
Slicing character arrays
A subsection of an array is called a slice. We can take slices of character arrays as well:
MATLAB
>> element = 'oxygen';
>> disp("first three characters: " + element(1:3))
>> disp("last three characters: " + element(4:6))
OUTPUT
first three characters: oxy
last three characters: gen
And we can use all the tricks we have learned to select the data we want. For example, to select every other character we can use the colon operator with an increment of 2:
OUTPUT
ans =
'oye'
We can also use the colon operator to access all the elements of the
array, but you’ll notice that the only difference between evaluating
element
and element(:)
is that the former is a
row vector, and the latter a column vector.
Key Points
- Some functions to initialize matrices include
zeros
,ones
, andrand
. They all produce a square matrix if only one argument is given, but you can specify the dimensions you want separated by a comma, as inzeros(rows,columns)
. - To select data points we use round brackets and provide the row and
column indices of the elements we want. They can be just numbers or
arrays of numbers, e.g.
M(5,[3,4,5])
. - Use the colon operator
:
to generate ordered arrays asstart:end
orstart:increment:end
. - Use the keyword
end
to obtain the index of the final element. - The colon operator by itself
:
selects all the elements.