02 Matrix & Vector Basics (Indexing & Concatenation)

Overview: The Matrix Unit

In MATLAB, everything is an array. This note covers array layouts, 1-based subscript indexing, column-major memory storage (linear indexing), horizontal/vertical concatenation, and transposes.


1. Array Construction & The Colon Operator

Arrays can be rows, columns, or multidimensional tables.

  • Row Vector: Elements separated by spaces or commas: r = [1, 2, 3] or r = [1 2 3].
  • Column Vector: Elements separated by semicolons: c = [1; 2; 3].
  • Matrix: Combination of row and column notation: M = [1 2; 3 4].

The Colon Operator (:)

Used to generate sequences of regularly spaced values.

  • Syntax: x = start:end or x = start:step:end
  • Parameters:
    • start: Initial value.
    • step (Optional): Increment step size (default is 1). Can be negative.
    • end: Terminal value boundary.
  • Examples:
    • 1:5 [1, 2, 3, 4, 5]
    • 1:2:9 [1, 3, 5, 7, 9]

2. 1-Based and Slice Indexing

MATLAB uses 1-Based Indexing. The first element is at index 1, not 0.

  • Subscript Indexing: A(row, column) retrieves the element at the specified intersection.
  • Slice range: A(r_start:r_end, c_start:c_end) extracts a submatrix.
  • End keyword: The keyword end evaluates to the index of the last element along that dimension (e.g. A(2:end, :)).

3. Linear Indexing (Column-Major Storage)

Under the hood, MATLAB stores multidimensional arrays in a single, contiguous memory block in Column-Major Order (down columns first, then across rows).

Matrix A (3x3):            Memory Layout:
[ 1  4  7 ]                [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ 2  5  8 ]                Idx: 1  2  3  4  5  6  7  8  9
[ 3  6  9 ]

Linear Indexing Syntax:

You can index into a multidimensional matrix with a single integer A(index). The index counts down columns sequentially.

  • For the matrix above, A(4) resolves to 4, and A(6) resolves to 6.

Key Functions:

  • sub2ind
    • Syntax: linearIdx = sub2ind(matrixSize, rowSub, colSub)
    • Description: Converts matrix row/column subscripts into equivalent linear indices.
  • ind2sub
    • Syntax: [rowSub, colSub] = ind2sub(matrixSize, linearIdx)
    • Description: Converts linear indices back into equivalent row and column subscripts.

4. Concatenation and Deletion

  • Horizontal Concatenation: [A, B] or [A B] (same as calling horzcat(A, B)). Dimensions must match along rows.
  • Vertical Concatenation: [A; B] (same as calling vertcat(A, B)). Dimensions must match along columns.
  • Deleting Elements: Assign empty brackets [] to remove entire rows or columns: A(1, :) = [] deletes the first row.

5. Transpose Operations (Real vs. Complex)

MATLAB makes a critical distinction when transposing arrays:

  • Hermitian Transpose / Complex Conjugate Transpose (A'): Swaps rows and columns and negates the signs of the imaginary parts (takes the complex conjugate). Equivalent function is ctranspose(A).
  • Dot Transpose / Standard Transpose (A.'): Swaps rows and columns without altering imaginary parts. Equivalent function is transpose(A).

Complex Matrix Danger

If matrix A contains complex numbers (e.g., 3 + 4i), using A' will change the signs of the imaginary elements. Always use A.' if you only want to transpose physical indices without altering complex values.


💻 Conceptual Code Demonstration

% 1. Create a Matrix
M = [1 4 7; 2 5 8; 3 6 9];
 
% 2. Slice notation and indexing
subMatrix = M(1:2, 2:end); % Extracts row 1-2, columns 2-3
 
% 3. Linear Index Conversion
sz = size(M);
linIdx = sub2ind(sz, 3, 2); % Row 3, Column 2 -> should resolve to 6
[r, c] = ind2sub(sz, 6);
 
fprintf('Subscripts for linear index 6: Row %d, Col %d\n', r, c);
 
% 4. Real vs Complex Transpose
z = [1 + 2i, 3 - 4i];
trans_conj = z';   % Result: [1-2i; 3+4i] (Hermitian)
trans_real = z.';  % Result: [1+2i; 3-4i] (Standard)