100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Indexing and Slicing in MATLAB

Master subscript and linear indexing, colon-based slicing, the end keyword, and logical indexing to access and filter matrix data efficiently.

Matrices & ArraysBeginner8 min readJul 10, 2026
Analogies

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.

matlab
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 > 50

MATLAB 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

Was this page helpful?

Topics covered

#Programming#MATLABStudyNotes#IndexingAndSlicingInMATLAB#Indexing#Slicing#MATLAB#Linear#SQL#StudyNotes#SkillVeris