Skip to content

MATLAB - Array Indexing

In MATLAB, everything is an array. Efficiently accessing and modifying parts of these arrays is a fundamental skill. MATLAB provides several powerful indexing techniques: subscript indexing, linear indexing, and logical indexing. Understanding these methods is key to writing clean, efficient, and ‘MATLAB-idiomatic’ code.

Unlike many programming languages like Python, C++, or Java that use 0-based indexing, MATLAB uses 1-based indexing. This means the first element of any array dimension is at index 1, not 0. This is a common source of errors for new users.

This is the most intuitive way to access elements in a multi-dimensional array, using a separate index for each dimension.

% Create a 3x4 matrix for our examples
A = [10 20 30 40; 50 60 70 80; 90 100 110 120]
A =
10 20 30 40
50 60 70 80
90 100 110 120
% Access the element at the 2nd row, 3rd column
element = A(2, 3)
element =
70

MATLAB also allows you to access array elements with a single index, as if the entire array were a single column vector. The elements are numbered down the columns first. This is called linear indexing.

For our matrix A, the linear indices are:

1 4 7 10 (Indices)
2 5 8 11
3 6 9 12
10 20 30 40 (Values)
50 60 70 80
90 100 110 120
% Access the 6th element in column-major order
A(6)
ans =
100

You can convert between subscript and linear indices using the sub2ind and ind2sub functions, which is useful in advanced applications.

The colon operator is a powerful tool for selecting ranges of elements, or ‘slicing’ an array. It can be used to select entire rows, columns, or sub-arrays.

SyntaxDescription
A(:, c)Selects all rows in column c.
A(r, :)Selects all columns in row r.
A(:)Selects all elements of A, returning them as a single column vector (using linear indexing).
A(r1:r2, c1:c2)Selects the sub-matrix from row r1 to r2 and column c1 to c2.
A(end, :)The end keyword refers to the last index in a dimension. This selects the last row.
% Get the entire 2nd column of A
col2 = A(:, 2)
col2 =
20
60
100
% Get the 3rd row of A
row3 = A(3, :)
row3 =
90 100 110 120
% Get a 2x2 sub-matrix from the bottom right corner
subA = A(2:end, 3:end)
subA =
70 80
110 120

This is arguably the most powerful indexing technique. You can use a logical array (an array of true and false values) of the same size as the array you are indexing to select elements where the logical array is true.

This is most commonly used by creating the logical array directly from a condition.

% Create a logical mask for all elements in A greater than 75
is_large = A > 75
is_large =
3×4 logical array
0 0 0 0
0 0 0 1
1 1 1 1
% Use this logical mask to select the corresponding elements from A
large_values = A(is_large)
large_values =
90
100
110
80
120

You can also combine this into a single, highly readable line:

large_values = A(A > 75)

Logical indexing is fantastic for data manipulation. For example, let’s find all values over 100 in our matrix A and replace them with the value -1.

A(A > 100) = -1
A =
10 20 30 40
50 60 70 80
90 100 -1 -1

This single line of code is far more efficient and readable than writing a nested for loop to achieve the same result.