Matrix Operations and Linear Algebra Basics
Implicit expansion (MATLAB R2016b+, Octave): an operation between a column vector and a row vector (or a matrix and a vector) automatically expands the singleton dimensions — [1; 2; 3] + [10 20] gives a 3×2 matrix.
Concept
Columns of A = rows of B Same size (or compatible for implicit expansion) Determinant, trace, rank det(A), trace(A), rank(A) Rarely needed directly Solve A x = b
Gaussian elimination / factorization — faster and more accurate than inv(A) * b [V, D] = eig(A) A * V = V * D Norm, dot, cross norm(v), dot(a, b), cross(a, b)
Implicit expansion (MATLAB R2016b+, Octave): an operation between a column vector and a row vector (or a matrix and a vector) automatically expands the singleton dimensions — [1; 2; 3] + [10 20] gives a 3×2 matrix.
Why It Matters
Circuit analysis (nodal/mesh equations), structural analysis (stiffness matrices), control (state-space models), statistics (regression) and computer graphics all reduce to linear algebra.
Syntax
x = A \ b; % solve A*x = b
[V, D] = eig(A); % eigen-decomposition
C = A * B; % matrix productExample
6.1 Products, solving systems, eigenvalues and implicit expansion
A = [2 1; 1 3];
B = [1 0; 2 1];
disp('A * B ='), disp(A * B)
disp('A .* B ='), disp(A .* B)
fprintf('det(A) = %g, trace(A) = %g, rank(A) = %d\n', det(A), trace(A), rank(A))
disp('inv(A) ='), disp(inv(A))
% solve A x = b: use backslash, not inv
b = [3; 5];
x = A \ b;
fprintf('solution of A x = b: x = [%g; %g], check A*x = [%g; %g]\n', x, A * x)
[V, D] = eig(A);
fprintf('eigenvalues: %.4f and %.4f\n', diag(D))
fprintf('A*v1 - lambda1*v1 (should be ~0): %g\n', norm(A * V(:, 1) - D(1, 1) * V(:, 1)))
fprintf('norm([3 4]) = %g, dot([1 2 3],[4 5 6]) = %g\n', norm([3 4]), dot([1 2 3], [4 5 6]))
disp('cross([1 0 0],[0 1 0]) ='), disp(cross([1 0 0], [0 1 0]))
% implicit expansion (broadcasting): column + row
disp('[1; 2; 3] + [10 20] ='), disp([1; 2; 3] + [10 20])Expected Result
A * B and A .* B are completely different. The system was solved with A \ b and the check A * x reproduced b. The eigenvector check A v − λ v is zero up to rounding.
A * B =
4 1
7 3
A .* B =
2 0
2 3
det(A) = 5, trace(A) = 5, rank(A) = 2
inv(A) =
0.6000 -0.2000
-0.2000 0.4000
solution of A x = b: x = [0.8; 1.4], check A*x = [3; 5]
eigenvalues: 1.3820 and 3.6180
A*v1 - lambda1*v1 (should be ~0): 2.48253e-16
norm([3 4]) = 5, dot([1 2 3],[4 5 6]) = 32
cross([1 0 0],[0 1 0]) =
0 0 1
[1; 2; 3] + [10 20] =
11 21
12 22
13 23Common Mistakes
inv(A) * b instead of A \ b (slower, less accurate, fails for singular/ill-conditioned matrices in worse ways).
Confusing A / B (right division, A * inv(B)) with A \ B.
Dimension mismatch errors from row/column orientation.
Relying on det(A) == 0 to test singularity (use rank or cond).
Best Practices
Solve systems with \; check cond(A) when results look suspicious.
Keep the physical meaning of rows and columns documented.
Use implicit expansion deliberately; check sizes with size when unsure.
Interview Relevance
High: "how do you solve a linear system in MATLAB?", "why not inv?", "what does eig return?", "A/B vs A\B?".
Practice Questions
Solve 2x + y - z = 8, -3x - y + 2z = -11, -2x + y + 2z = -3.
Verify A * V = V * D for [V, D] = eig(magic(3)).
Compute the angle between two vectors using dot and norm.
Create a multiplication table 1..9 using implicit expansion.
Quick Quiz
Q1. (Multiple choice) The recommended way to solve A x = b is: A) inv(A) * b B) A \ b C) b / A D) A ^ -1 * b Answer: B.
Q2. (Predict the output) size([1; 2; 3] + [10 20]) Answer: 3 2 — implicit expansion (verified).
Q3. (True/False) A * B and A .* B give the same result for 2×2 matrices.
Answer: False (verified).
Q4. (Find the bug) x = b \ A; to solve A x = b.
Answer: Operand order is reversed: x = A \ b.
Coding Challenge
Solve the 3-equation system from Practice Question 1 with \, print the solution, and verify it by printing the residual norm.
A = [2 1 -1; -3 -1 2; -2 1 2];
b = [8; -11; -3];
x = A \ b;
fprintf('x = %g, y = %g, z = %g\n', x)
fprintf('residual norm: %.2e\n', norm(A * x - b))