MATLAB - Array Indexing
MATLAB - Array Indexing
Section titled “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.
Important Note: 1-Based Indexing
Section titled “Important Note: 1-Based Indexing”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.
1. Subscript Indexing
Section titled “1. Subscript Indexing”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 examplesA = [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 columnelement = A(2, 3)
element = 702. Linear Indexing
Section titled “2. Linear Indexing”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 orderA(6)
ans = 100You can convert between subscript and linear indices using the sub2ind and ind2sub functions, which is useful in advanced applications.
3. Colon Operator (:) and Slicing
Section titled “3. Colon Operator (:) and Slicing”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.
| Syntax | Description |
|---|---|
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. |
Slicing Examples
Section titled “Slicing Examples”% Get the entire 2nd column of Acol2 = A(:, 2)
col2 = 20 60 100
% Get the 3rd row of Arow3 = A(3, :)
row3 = 90 100 110 120
% Get a 2x2 sub-matrix from the bottom right cornersubA = A(2:end, 3:end)
subA = 70 80 110 1204. Logical Indexing
Section titled “4. Logical Indexing”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 75is_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 Alarge_values = A(is_large)
large_values =
90 100 110 80 120You can also combine this into a single, highly readable line:
large_values = A(A > 75)Practical Application: Data Cleaning
Section titled “Practical Application: Data Cleaning”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 -1This single line of code is far more efficient and readable than writing a nested for loop to achieve the same result.