How to Use Indexing in MATLAB: Syntax and Examples
In MATLAB, you use
indexing to access or change elements of arrays or matrices by specifying their position inside parentheses, like A(row, column). You can use single indices for vectors or multiple indices for matrices, and also logical or colon operators for flexible selection.Syntax
Indexing in MATLAB uses parentheses () with one or more indices inside to select elements from arrays or matrices.
A(i): Access thei-th element of a vectorA.A(i,j): Access the element in thei-th row andj-th column of matrixA.A(start:end): Use colon operator to select a range of elements.A(logicalIndex): Use a logical array to select elements where the condition is true.
matlab
A = [10, 20, 30, 40, 50]; element = A(3); % Access third element M = [1, 2, 3; 4, 5, 6; 7, 8, 9]; element_matrix = M(2,3); % Access element at row 2, column 3 range = A(2:4); % Access elements from index 2 to 4 logicalIndex = A > 25; selected = A(logicalIndex); % Elements greater than 25
Example
This example shows how to create a matrix and use different types of indexing to access and modify its elements.
matlab
M = [5, 10, 15; 20, 25, 30; 35, 40, 45]; % Access element at row 1, column 2 val1 = M(1, 2); % Access entire second row row2 = M(2, :); % Access entire third column col3 = M(:, 3); % Modify element at row 3, column 1 M(3, 1) = 100; % Logical indexing: elements greater than 20 greaterThan20 = M(M > 20);
Output
val1 = 10
row2 = [20 25 30]
col3 = [15; 30; 45]
M =
5 10 15
20 25 30
100 40 45
greaterThan20 = [25 30 100 40 45]
Common Pitfalls
Common mistakes when using indexing in MATLAB include:
- Using parentheses
()instead of curly braces{}for cell arrays. - Indexing outside the array bounds, which causes errors.
- Confusing linear indexing with row-column indexing in matrices.
- Forgetting that MATLAB indices start at 1, not 0.
matlab
A = [1, 2, 3]; % Wrong: Using 0 as index (MATLAB indices start at 1) % val = A(0); % This will cause an error % Correct: val = A(1); % Access first element % Wrong: Using parentheses for cell array content C = {10, 20, 30}; % val = C(2); % Returns a cell, not the content % Correct: val_content = C{2}; % Access content inside the cell
Output
val = 1
val_content = 20
Quick Reference
| Indexing Type | Syntax | Description |
|---|---|---|
| Single element | A(i) | Access the i-th element of a vector or linear index in matrix |
| Row and column | A(i,j) | Access element at row i and column j in a matrix |
| Range | A(start:end) | Access elements from start to end indices |
| All elements in row/column | A(i,:) | All columns in row i |
| All elements in column | A(:,j) | All rows in column j |
| Logical indexing | A(logicalArray) | Select elements where logicalArray is true |
Key Takeaways
MATLAB indexing uses parentheses with one or more indices to access array elements.
Indices start at 1, not 0, so always use positive integers starting from 1.
Use colon operator ':' to select ranges or entire rows/columns easily.
Logical indexing lets you select elements based on conditions.
For cell arrays, use curly braces '{}' to access content, not parentheses.