Eigenvalues and eigenvectors (eig) in MATLAB - Time & Space Complexity
When we find eigenvalues and eigenvectors of a matrix, we want to know how long it takes as the matrix gets bigger.
We ask: How does the work grow when the matrix size increases?
Analyze the time complexity of the following code snippet.
A = rand(n); % Create an n-by-n random matrix
[V, D] = eig(A); % Compute eigenvectors and eigenvalues
This code creates a square matrix and finds its eigenvalues and eigenvectors.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The algorithm internally performs matrix factorizations and iterative procedures.
- How many times: These operations depend on the matrix size n, often involving nested loops over n.
As the matrix size n grows, the number of calculations grows quickly because the algorithm works with all rows and columns together.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1,000 operations |
| 100 | About 1,000,000 operations |
| 1000 | About 1,000,000,000 operations |
Pattern observation: The work grows roughly by the cube of n, so doubling n makes the work about eight times bigger.
Time Complexity: O(n^3)
This means if the matrix size doubles, the time to find eigenvalues and eigenvectors increases about eight times.
[X] Wrong: "Finding eigenvalues is as fast as just scanning the matrix once."
[OK] Correct: The process involves complex calculations that look at all parts of the matrix many times, so it takes much longer than a simple scan.
Understanding how eigenvalue computations scale helps you explain performance in real projects and shows you know how algorithms behave with bigger data.
"What if we only want eigenvalues but not eigenvectors? How would the time complexity change?"