Linear and Subscript Indexing
MATLAB supports two complementary ways to access elements: subscript indexing A(row,col) picks an element by its row and column position, while linear indexing A(k) treats the entire matrix as one long column-major vector, numbering elements column by column from top-left. For a 3x3 matrix, A(2,1) and A(2) refer to the same element because MATLAB stores data column-major.
Cricket analogy: Finding a specific delivery by 'over 4, ball 2' is subscript indexing, like A(4,2), while numbering every ball of the innings 1 through 300 straight through is linear indexing, letting you check ball 187 regardless of which over it falls in.
The Colon Operator for Slicing
A colon alone within a subscript means 'every element along this dimension': A(:,2) returns the entire second column, A(3,:) returns the entire third row, and A(:) reshapes the whole matrix into a single column vector. Ranges can be combined with the colon operator, so A(1:2,2:3) extracts the top-right 2x2 sub-block.
Cricket analogy: A(:,2) pulling every value in column 2 is like requesting every batsman's score in the second innings across the whole scorecard, while A(3,:) pulling row 3 is like pulling one bowler's entire figures across every over.
Logical Indexing and the end Keyword
The keyword end refers to the last index along a dimension, so A(end,end) always grabs the bottom-right corner regardless of size, and A(2:end,:) drops the first row. Logical indexing uses a same-size boolean array to filter: A(A>5) returns a column vector of every element exceeding 5, which is invaluable for conditional selection without writing a loop.
Cricket analogy: A(end,end) is like always referring to the very last ball of the very last over of an innings no matter how many overs were bowled, while A(A>5) filtering for scores above 5 mirrors picking out every player who scored a boundary.
A = [10 20 30; 40 50 60; 70 80 90];
A(2,1) % subscript indexing -> 20
A(4) % linear indexing (column-major) -> 20
col2 = A(:,2); % entire second column
row3 = A(3,:); % entire third row
block = A(1:2,2:3); % top-right 2x2 sub-block
corner = A(end,end); % bottom-right element -> 90
noFirstRow = A(2:end,:); % drop the first row
big = A(A>50); % logical indexing -> column vector of values > 50MATLAB stores matrices in column-major order internally, which is why linear indexing A(k) counts down column 1 first, then column 2, and so on — this matters when reshaping or flattening with A(:).
MATLAB indexing starts at 1, not 0. A(0) is invalid and throws an error; the first element is always A(1) or A(1,1). Porting code from Python or C without adjusting index offsets by one is a very common source of off-by-one bugs.
- Subscript indexing A(row,col) and linear indexing A(k) both access matrix elements; MATLAB is column-major internally.
- A colon alone in a dimension (A(:,2), A(3,:)) selects every element along that dimension.
- A(:) reshapes any matrix into a single column vector.
- The keyword end refers to the last index along a dimension and adapts automatically to matrix size.
- Logical indexing A(A>5) returns only elements satisfying a condition, without an explicit loop.
- MATLAB indexing is 1-based; there is no A(0).
Practice what you learned
1. For a 3x3 matrix A, what does A(:,2) return?
2. What is the index of the first element in a MATLAB array?
3. What does A(A>10) return?
4. For a matrix A, what does A(end,:) select?
5. MATLAB linear indexing counts elements in which order?
Was this page helpful?
You May Also Like
Matrices and Vectors in MATLAB
Learn how MATLAB represents scalars, vectors, and matrices as a single unified array type, and how to construct them with literals and built-in functions.
Vectorization and Broadcasting
Learn to replace explicit loops with whole-array operations and use MATLAB's implicit expansion (broadcasting) rules for faster, more concise code.
Matrix Operations and Linear Algebra
Understand the distinction between matrix and element-wise operators, solve linear systems with backslash, and compute determinants, inverses, and eigenvalues.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics