Vectors and Matrices

size(A), size(A, dim), numel(A), length(A) (largest dimension), ndims(A), isempty(A), isrow, iscolumn, isvector, isscalar.

Concept

[1 2 3] or [1, 2, 3] 2×2 matrix (; starts a new row) a:b, a:step:b Range (colon operator) linspace(a, b, n) n equally spaced points including both ends zeros(m, n), ones(m, n), eye(n) Constant and identity matrices rand, randn, randi

Uniform, normal, integer random numbers (seed with rng) magic(n), diag(v) [A B], [A; B] Horizontal / vertical concatenation

size(A), size(A, dim), numel(A), length(A) (largest dimension), ndims(A), isempty(A), isrow, iscolumn, isvector, isscalar.

A' transposes; reshape(A, m, n) rearranges elements (column by column); A(:) makes a column vector; repmat repeats; fliplr, flipud, rot90, circshift rearrange.

MATLAB stores arrays in column-major order: elements of the first column come first in memory. reshape and linear indexing (Module 5) follow this order.

Why It Matters

Measurement data, images, signals and system matrices are all arrays. Building them correctly and knowing their orientation (row or column) avoids a large class of dimension errors.

Syntax

t = 0:0.001:1;              % 1001 samples
A = zeros(3, 4);            % preallocate
M = [A; ones(1, 4)];        % append a row
v = linspace(-1, 1, 5)';    % column vector

Example

4.1 Creating and describing arrays (No semicolons here, so each result is displayed — in Octave's compact format.)

row = [1 2 3 4]
col = [1; 2; 3]
M = [1 2 3; 4 5 6]
r = 0:0.25:1
l = linspace(0, 1, 5)
Z = zeros(2, 3)
O = ones(2)
I = eye(3)
fprintf('size(M) = [%d %d], numel(M) = %d, length(row) = %d, ndims(M) = %d\n', size(M), numel(M), length(row), ndims(M))
T = M'
R = reshape(1:6, 2, 3)
C = [M; 7 8 9]
rng(0); rnd = rand(1, 3);
fprintf('rand values lie in (0,1): %d\n', all(rnd > 0 & rnd < 1))

Expected Result

0:0.25:1 and linspace(0, 1, 5) produce the same five values; reshape(1:6, 2, 3) filled the matrix column by column (1 2 in the first column). Octave labels eye(3) as a "Diagonal Matrix" because it stores it specially; MATLAB shows a normal 3×3 matrix with the same values.

row =

   1   2   3   4

col =

   1
   2
   3

M =

   1   2   3
   4   5   6

r =

         0    0.2500    0.5000    0.7500    1.0000

l =

        0   0.2500   0.5000   0.7500   1.0000

Z =

   0   0   0
   0   0   0

O =

   1   1
   1   1

I =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

size(M) = [2 3], numel(M) = 6, length(row) = 4, ndims(M) = 2
T =

   1   4
   2   5
   3   6

R =

   1   3   5
   2   4   6

C =

   1   2   3
   4   5   6
   7   8   9

rand values lie in (0,1): 1

Common Mistakes

Using commas vs semicolons incorrectly ([1, 2; 3] is an error: rows of different length).

Floating-point ranges: 0:0.1:1 may not end exactly at 1 in some step combinations; use linspace when the end point matters.

Mixing row and column vectors in element-wise operations (with implicit expansion, row + col silently becomes a matrix!).

Using length when you mean numel or size(A, 1).

Best Practices

Preallocate with zeros/ones before filling in loops.

Be consistent: store signals as column vectors (one column per channel), which matches most MATLAB functions.

Seed random numbers (rng(0)) for reproducible experiments.

Interview Relevance

Medium: "row vs column vector?", "linspace vs colon?", "what is column-major order?", "length vs numel vs size?".

Practice Questions

Create the 4×4 matrix with 1..16 filled row by row (hint: reshape then transpose).

Create a 5×5 matrix with 2 on the diagonal and -1 above and below it.

Append a column of ones to a 3×2 matrix.

Generate 10 random integers between 1 and 6 with a fixed seed.

Quick Quiz

Q1. (Multiple choice) Which creates a column vector? A) [1 2 3] B) [1, 2, 3] C) [1; 2; 3] D) 1:3 Answer: C.

Q2. (Predict the output) size(reshape(1:6, 2, 3)) Answer: 2 3 (verified).

Q3. (True/False) MATLAB stores matrices row by row in memory.

Answer: False. Column-major.

Q4. (Find the bug) A = [1 2 3; 4 5]; Answer: Rows must have the same number of elements ("vertical dimensions mismatch").

Coding Challenge

Build the tridiagonal 5×5 matrix with 2 on the main diagonal and −1 on the first sub- and super-diagonals using diag, and print it.

n = 5;
T = 2 * eye(n) - diag(ones(1, n - 1), 1) - diag(ones(1, n - 1), -1);
disp(T)
fprintf('symmetric: %d, size %dx%d\n', isequal(T, T'), size(T))