Array Indexing
Every variable in MATLAB® is an array that can hold many numbers. When you want to access selected elements of an array, use indexing.
For example, consider the 4-by-4 matrix A
:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A = 4×4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as
A(4,2)
ans = 14
Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:
A(8)
ans = 14
Using a single subscript to refer to a particular element in an array is called linear indexing.
If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error.
test = A(4,5)
Index in position 2 exceeds array bounds (must not exceed 4).
However, on the left side of an assignment statement, you can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers.
A(4,5) = 17
A = 4×5
1 2 3 4 0
5 6 7 8 0
9 10 11 12 0
13 14 15 16 17
To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end
. For example, list the elements in the first three rows and the second column of A
:
A(1:3,2)
ans = 3×1
2
6
10
The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A
:
A(3,:)
ans = 1×5
9 10 11 12 0
The colon operator also allows you to create an equally spaced vector of values using the more general form start:step:end
.
B = 0:10:100
B = 1×11
0 10 20 30 40 50 60 70 80 90 100
If you omit the middle step, as in start:end
, MATLAB uses the default step value of 1
.