Element-wise vs Matrix Arithmetic
MATLAB distinguishes sharply between matrix arithmetic and element-wise arithmetic through its operator syntax: * performs true matrix multiplication (requiring inner dimensions to match), while .* performs element-wise multiplication (requiring identical dimensions or broadcast-compatible shapes). The same dot-prefix convention applies to division (/ vs ./) and power (^ vs .^), so A.^2 squares every element individually while A^2 computes A multiplied by itself as matrices.
Cricket analogy: A*B, true matrix multiplication, is like combining a batting lineup's strike rates with a bowling attack's economy rates through a structured net-run-rate formula, whereas A.*B, element-wise, is simply multiplying each player's own two stats together independently.
Solving Linear Systems
To solve a linear system Ax = b, MATLAB provides the backslash operator: x = A\b computes the solution using an appropriate factorization (LU for square systems, QR or least-squares for over/under-determined systems) far more efficiently and stably than explicitly computing inv(A)*b. For a well-posed square system this is both faster and more numerically robust than inverting the matrix.
Cricket analogy: x = A\b solving for the optimal batting order to hit a target score is like using a direct backward calculation rather than trial-and-error, computing the answer directly instead of testing every possible lineup like inv(A)*b would.
Determinants, Inverses, and Eigenvalues
det(A) computes the determinant, revealing whether A is singular (det = 0, non-invertible); inv(A) computes the explicit matrix inverse; and eig(A) returns the eigenvalues (and optionally eigenvectors via [V,D] = eig(A)) that satisfy Av = lambda*v. These quantities underpin stability analysis, PCA, and systems of differential equations, and MATLAB computes them using robust LAPACK routines rather than naive formulas.
Cricket analogy: det(A) = 0 signaling a singular, non-invertible matrix is like a tied match with no valid net-run-rate tiebreaker possible, while eig(A) revealing the dominant eigenvalue is like identifying the single player whose form dominates the team's overall trend.
A = [2 1; 1 3];
B = [1 0; 2 1];
C = A * B; % true matrix multiplication
D = A .* B; % element-wise multiplication
E = A .^ 2; % element-wise square
F = A ^ 2; % A*A, matrix power
b = [5; 10];
x = A \ b; % solve Ax = b (preferred over inv(A)*b)
d = det(A); % determinant
Ai = inv(A); % explicit inverse
[V, D2] = eig(A); % eigenvectors V, eigenvalues on diag(D2)Prefer A\b over inv(A)*b for solving linear systems: the backslash operator selects a numerically stable factorization (LU, QR, or Cholesky depending on A's structure) and avoids the extra rounding error and computational cost of forming an explicit inverse.
A near-singular matrix (very small but nonzero determinant) can still produce wildly inaccurate results from inv(A) due to floating-point rounding — MATLAB will warn that the matrix is close to singular or badly scaled. Check cond(A), the condition number, rather than relying on det(A) alone to judge numerical reliability.
- * is true matrix multiplication; .* is element-wise — the same dot-prefix distinction applies to / vs ./ and ^ vs .^.
- x = A\b solves Ax=b using a stable factorization and is preferred over inv(A)*b.
- det(A) reveals singularity (det=0 means non-invertible); inv(A) computes the explicit inverse.
- eig(A) returns eigenvalues; [V,D]=eig(A) returns eigenvectors and eigenvalues satisfying Av=lambda*v.
- A small determinant does not necessarily mean a matrix is poorly conditioned — use cond(A) instead.
- MATLAB's linear algebra routines are backed by LAPACK for numerical robustness.
Practice what you learned
1. What is the difference between A*B and A.*B?
2. What is the preferred way to solve Ax=b in MATLAB?
3. What does det(A) = 0 indicate?
4. What does [V,D] = eig(A) return?
5. Why might cond(A) be preferred over det(A) to judge numerical stability?
Was this page helpful?
You May Also Like
Matrices and Vectors in MATLAB
Learn how MATLAB represents scalars, vectors, and matrices as a single unified array type, and how to construct them with literals and built-in functions.
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.
Vectorization and Broadcasting
Learn to replace explicit loops with whole-array operations and use MATLAB's implicit expansion (broadcasting) rules for faster, more concise code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics