Written and maintained by CASRAI Editorial Board
Last updated
MATLAB indexing selects elements of an array by position: A(3,2) is row 3, column 2, and the first element of an array is index 1, not 0. This guide maps each indexing task (subscripts, ranges, linear indices, logical masks, find, cell arrays) to the syntax that does it, using outputs taken from the MathWorks documentation.
What it is used for in research
Analysts index arrays to slice a data matrix into trials or channels, to keep only samples that pass a threshold, to locate where a value occurs, and to pull results out of cell arrays. Getting indexing wrong rarely raises an error. It more often returns the wrong subset, which is a reproducibility problem as much as a coding one (see computational reproducibility). Related MATLAB basics are covered in linspace in MATLAB and MATLAB subplot layouts.
Install or access (official source only)
Array indexing is part of the core MATLAB language and is documented under the base MATLAB help at mathworks.com/help/matlab. MATLAB is distributed by MathWorks; obtain it and its licence terms from mathworks.com or through your institution’s licence administrator.
MATLAB indexing: which syntax for which task
All outputs below are from the MathWorks documentation pages listed under Sources. With A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]:
| Task | Syntax | Result |
|---|---|---|
| One element (row, column) | A(3,2) | 10 |
| Chosen columns of one row | A(2,[1 3]) | 5 7 |
| Range, up to the last column | A(1:3,2:end) | [2 3 4; 6 7 8; 10 11 12] |
| Whole row | A(3,:) | 9 10 11 12 |
| Second-to-last column | A(:,end-1) | [3; 7; 11; 15] |
Step-by-step: index an array
Does MATLAB use zero indexing?
No. The documentation examples count from 1: with A = [12 36 91; 45 29 48; 33 25 11], A(6) returns 25, the sixth element. Python’s NumPy documentation, by contrast, describes its indexing as 0-based, so code ported between the two needs its indices shifted.
- Subscript indexing. Give one index per dimension: A(3,2) is row 3, column 2. Pass vectors of indices or a colon range to select several rows or columns, and use a lone colon for all rows or all columns.
- Use end for the last position. The keyword end stands for the last row or column inside an index, so A(:,end-1) is the second-to-last column.
- Linear indexing. One index counts through the array column by column, left to right. For the A above (3-by-3), A(6) is 25 and sum(A(:)) is 330. Convert with sub2ind(size(A),3,2), which returns 6, and [row,col] = ind2sub(size(A),6), which returns 3 and 2.
- Logical indexing. A comparison returns a logical array; using it as an index returns the elements where it is 1. With A = [1 2 6; 4 3 6] and B = [0 3 7; 3 7 5], ind = A < B is [0 1 1; 0 1 0], A(ind) returns [2; 3; 6] and B(ind) returns [3; 7; 7]. The operator ~ inverts a logical array.
Finding indices with find
find(X) returns the linear indices of nonzero elements. For X = [1 0 2; 0 1 1; 0 0 4] it returns [1; 5; 7; 8; 9], and find(~X) returns [2; 3; 4; 6]. This covers the common searches for the first, last or several positions of a value or a condition.
- First n matches: with X = magic(4), find(X<10,5) returns [2; 3; 4; 5; 7], and X(k) with that k gives [5; 9; 4; 2; 7].
- Last n matches: find(X,n,’last’) (‘first’ is the default). With X = magic(6) after X(1:2:end) = 0, find(X,4,’last’) returns [30; 32; 34; 36].
- Index of a value: use a comparison inside find, for example find(X == value), which returns linear indices.
find versus logical indexing
Logical indexing returns the elements themselves directly (A(A<B)), while find returns positions you can store, count or reuse. The output of find is a row vector if X is a row vector and a column vector otherwise.
What does {} mean in MATLAB?
Braces create cell arrays and index into them. In a cell array C, parentheses return a subset that is still a cell array, while braces return the contents of the cell: C{2,3} returns the numeric matrix stored there. C{1:2,1:2} produces a comma-separated list, so [v1,v2,v3,v4] = C{1:2,1:2} assigns the four cells’ contents to four variables, and v = [C{2,1:2}] concatenates the contents of two cells.
When to use * versus .* in MATLAB
The operators page lists * as matrix multiplication and .* as element-wise multiplication. Use .* when you want each element multiplied by the matching element of another array of the same size; use * for a true matrix product. This is an operator choice rather than indexing, but the two are routinely confused when working on indexed subsets.
Output and how to read it
- A single subscript returns a scalar; ranges return a sub-matrix with one row per row index and one column per column index.
- Logical indexing returns a column of the selected elements, in column-by-column order, as in A(ind) = [2; 3; 6] above.
- find returns positions, not values: use X(k) to get the values back.
Troubleshooting
- Result is shifted by one: the code was written with 0-based indexing in mind; MATLAB starts at 1.
- Wrong element from a single index: a single index is linear and runs down columns first, not across rows. Convert with ind2sub if you need row and column.
- Got a cell instead of a value: you used () on a cell array; use {} to read the contents.
- Element-wise result expected but matrix product returned: replace * with .*.
Free alternatives
| Tool | Indexing behaviour (from its documentation) |
|---|---|
| GNU Octave | Index components map to dimensions in order (rows first); a colon selects all elements of a dimension; end refers to the last entry of a dimension (A(end:-1:1) reverses A); a logical array returns the values at its true positions. |
| NumPy (Python) | Indexing is 0-based and accepts negative indices that count from the end; a boolean array of matching dimensions returns a one-dimensional array of the elements at its True positions. |
FAQ
Does MATLAB use zero indexing?
No. MATLAB documentation examples count from 1, so A(1) is the first element.
What does {} mean in MATLAB?
It creates cell arrays and indexes into them: C{2,3} returns the contents of that cell, whereas C(2,3) returns a 1-by-1 cell.
When to use * versus .* in MATLAB?
Use * for matrix multiplication and .* for element-wise multiplication.
How do I find the index of the first or last match?
find(X,1) returns the first and find(X,1,’last’) the last nonzero or true position, as linear indices.
Is MATLAB being phased out?
Nothing on MathWorks’ R2026a release page says so. That page, dated 2026, presents R2026a as the latest release of MATLAB and Simulink, invites users to download it, and links to release notes and license-related changes. It does not announce a discontinuation or retirement of MATLAB. Indexing syntax shown on this page is documented in the current MATLAB help. For changes to a specific function, check the release notes for your release.
Sources (official docs, read 2026-11-07)
- MathWorks, Array Indexing
- MathWorks, find
- MathWorks, Access Data in a Cell Array
- MathWorks, MATLAB Operators and Special Characters
- MathWorks, R2026a Release Highlights
- GNU Octave, Index Expressions
- NumPy, Indexing on ndarrays
Code-level work like this also benefits from code review of research software.







