Indexing

Concept

A(r, c) selects row r, column c. Either can be a vector or : (all). end means the last index in that dimension.

A([1 end], [1 end]) The four corners Indices start at 1, not 0.

A(k) treats the array as one long column (column-major): A(7) in a 4×4 matrix is row 3, column 2. sub2ind and ind2sub convert between the two.

Assign to a selection: A(1, :) = 0.

Assigning beyond the current size grows the array (padding with zeros).

Delete with []: v([2 4]) = [], A(:, 3) = [].

Logical indexing (selecting with true/false masks) is covered in Module 13.

Why It Matters

Indexing is how you extract channels from measurement matrices, windows from signals, regions from images, and rows from datasets. Off-by-one and orientation mistakes are common here.

Syntax

x = A(i, j);        A(i, :) = [];        v(end + 1) = value;
block = A(r1:r2, c1:c2);                 k = sub2ind(size(A), i, j);

Example

5.1 Subscript, linear and end indexing

A = magic(4)
fprintf('A(2,3) = %d\n', A(2, 3))
fprintf('A(7) (linear index, column-major) = %d\n', A(7))
row2 = A(2, :)
col1 = A(:, 1)'
sub = A(1:2, 3:4)
last = A(end, end)
corners = A([1 end], [1 end])
A(1, 1) = 0;
A(5, 5) = 99;                      % assigning outside the array grows it with zeros
disp(A)
v = 10:10:60;
v([2 4]) = []                      % delete elements
evens = v(2:2:end)
rev = v(end:-1:1)
[r, c] = ind2sub([4 4], 7);
fprintf('linear index 7 in a 4x4 is row %d, column %d; sub2ind(2,3) = %d\n', r, c, sub2ind([4 4], 2, 3))

Expected Result

A(7) counted down the columns (column 1 has elements 1–4, so element 7 is row 3 of column 2). Assigning A(5, 5) = 99 grew the 4×4 matrix to 5×5, padding with zeros.

A =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

A(2,3) = 10
A(7) (linear index, column-major) = 7
row2 =

    5   11   10    8

col1 =

   16    5    9    4

sub =

    3   13
   10    8

last = 1
corners =

   16   13
    4    1

    0    2    3   13    0
    5   11   10    8    0
    9    7    6   12    0
    4   14   15    1    0
    0    0    0    0   99
v =

   10   30   50   60

evens =

   30   60

rev =

   60   50   30   10

linear index 7 in a 4x4 is row 3, column 2; sub2ind(2,3) = 10

Common Mistakes

Using 0 as an index ("index must be a positive integer").

Forgetting end changes meaning per dimension (A(end, 1) is the last row).

Growing arrays accidentally by assigning to a large index (a typo like x(10000) = 1 creates 9 999 zeros).

Deleting elements inside a loop that iterates over the same array's indices.

Best Practices

Use end rather than hard-coded lengths.

Use size(A, 1) / size(A, 2) explicitly for rows/columns.

Use logical indexing for "select where condition holds" (Module 13).

Interview Relevance

Medium: "is MATLAB 0- or 1-indexed?", "what is linear indexing?", "how do you delete a row?", "what does end mean inside an index?".

Practice Questions

Extract every second row of a 6×6 matrix.

Swap the first and last columns of a matrix.

Replace the diagonal of magic(4) with zeros using sub2ind.

Reverse the order of the rows of a matrix.

Quick Quiz

Q1. (Multiple choice) In MATLAB the first element of v is: A) v(0) B) v(1) C) v[0] D) v{1} Answer: B.

Q2. (Predict the output) In a 4×4 matrix, which row and column is linear index 7?

Answer: Row 3, column 2 (verified).

Q3. (True/False) v([2 4]) = [] removes the second and fourth elements.

Answer: True (verified).

Q4. (Find the bug) last = v(length(v) + 1); Answer: Index exceeds the array bounds; the last element is v(end).

Coding Challenge

Given A = magic(5), extract the central 3×3 block, the anti-diagonal (top-right to bottom-left) and the sum of the border elements.

A = magic(5);
center = A(2:4, 2:4);
anti = A(sub2ind(size(A), 1:5, 5:-1:1));
border = sum(A(:)) - sum(center(:));
disp(center)
fprintf('anti-diagonal: %s\n', mat2str(anti))
fprintf('sum of the border elements: %d\n', border)