Matrix diagonalization is the process of reducing a square matrix into its diagonal form using a similarity transformation. This process is useful because diagonal matrices are easier to work with, especially when raising them to integer powers.
Not all matrices are diagonalizable. A matrix is diagonalizable if it has no defective eigenvalues, meaning each eigenvalue's geometric multiplicity is equal to its algebraic multiplicity.
Matrix DiagonalizationMatrix Similarity Transformation
Let A and B be two matrices of order n. Matrix B is considered similar to A if there exists an invertible matrix P such that:
B = P-1 A P
This transformation is known as Matrix Similarity Transformation. Similar matrices have the same rank, trace, determinant, and eigenvalues. Additionally, the eigenvalues of similar matrices maintain their algebraic and geometric multiplicities.
Diagonalization of a Matrix
Diagonalization of a matrix refers to the process of transforming any matrix A into its diagonal form D. According to the similarity transformation, if A is diagonalizable, then:
D=P^{-1} A P
where D is a diagonal matrix and P is a modal matrix.
A modal matrix is an n × n matrix consisting of the eigenvectors of A. It is essential in the process of diagonalization and similarity transformation.
In simpler words, it is the process of taking a square matrix and converting it into a special type of matrix called a diagonal matrix.
Steps to Diagonalize a Matrix
Step 1: Initialize the diagonal matrix D as:
D=\left[\begin{array}{ccc} \lambda_{1} & 0 & 0 \\ 0 & \lambda_{2} & 0 \\ 0 & 0 & \lambda_{3} \end{array}\right]
where λ1, λ2, λ3 -> eigen values
Step 2: Find the eigen values using the equation given below.
\operatorname{det}(A-\lambda I)=0
where, A -> given 3x3 square matrix.
I -> identity matrix of size 3x3.
λ -> eigen value.
Step 3: Compute the corresponding eigen vectors using the equation given below.
At λ=i
[A - \lambda I] X_i = 0
where, λi -> eigen value.
Xi -> corresponding eigen vector.
Step 4: Create the modal matrix P.
P=\left[X_{0} X_{1} . . X_{n}\right]
Here, all the eigenvectors till Xi have filled column-wise in matrix P.
Step 5: Find P-1 and then use the equation given below to find diagonal matrix D.
D=P^{-1} A P
Example Problem
Problem Statement: Assume a 3x3 square matrix A having the following values:
A=\left[\begin{array}{ccc} 1 & 0 & -1 \\ 1 & 2 & 1 \\ 2 & 2 & 3 \end{array}\right]
Find the diagonal matrix D of A using the diagonalization of the matrix. [ D = P-1AP ]
Solution:
Step 1: Initializing D as:
D=\left[\begin{array}{ccc} \lambda_{1} & 0 & 0 \\ 0 & \lambda_{2} & 0 \\ 0 & 0 & \lambda_{3} \end{array}\right]
Step 2: Find the eigen values. (or possible values of λ)
\operatorname{det}(A-\lambda I)=0
\begin{array}{l} \Longrightarrow \operatorname{det}(A-\lambda I)=\operatorname{det}\left(\left[\begin{array}{ccc} 1-\lambda & 0 & -1 \\ 1 & 2-\lambda & 1 \\ 2 & 2 & 3-\lambda \end{array}\right]\right)=0 \\ \Longrightarrow\left(\lambda^{3}-6 \lambda^{2}+11 \lambda-6\right)=0 & \\ \Longrightarrow(\lambda-1)(\lambda-2)(\lambda-3)=0 \end{array}
⟹\lambda=1,2,3
Step 3: Find the eigen vectors X1, X2, X3 corresponding to the eigen values λ = 1,2,3.
At λ=1
(A - (1) I) X_{1}=0
\Longrightarrow \begin{bmatrix} 1-1 & 0 & -1 \\ 1 & 2-1 & 1 \\ 2 & 2 & 3-1 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}
\Longrightarrow \begin{bmatrix} 0 & 0 & -1 \\ 1 & 1 & 1 \\ 2 & 2 & 2 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}
On solving, we get the following equations:
x_{3}=0(x_{1})
x_{1}+x_{2}=0 \Longrightarrow x_{2}=-x_{1}
\therefore X_{1}= \begin{bmatrix} x_{1} \\ -x_{1} \\ 0(x_{1}) \end{bmatrix}
\Longrightarrow X_{1}= \begin{bmatrix} 1 \\ -1 \\ 0 \end{bmatrix}
Similarly, for λ=2:
X_{2}= \begin{bmatrix} -2 \\ 1 \\ 2 \end{bmatrix}
And for λ=3 :
X_{3}= \begin{bmatrix} 1 \\ -1 \\ -2 \end{bmatrix}
Step 5: Creation of modal matrix P. (here, X1, X2, X3 are column vectors)
P=\left[X_{1} X_{2} X_{3}\right]=\left[\begin{array}{ccc} 1 & -2 & 1 \\ -1 & 1 & -1 \\ 0 & 2 & -2 \end{array}\right]
Step 6: Finding P-1 and then putting values in diagonalization of a matrix equation [D= P-1 AP]
We do Step 6 to find out which eigenvalue will replace λ1, λ2, and λ3 in the initial diagonal matrix created in Step 1.
\begin{array}{l} \begin{array}{l} \quad P=\left[\begin{array}{ccc} 1 & -2 & 1 \\ -1 & 1 & -1 \\ 0 & 2 & -2 \end{array}\right] \\\\ \operatorname{det}(P)=[0+(4)+(-2)] \\ =2 \end{array}\\\\ \text { Since } \operatorname{det}(P) \neq 0 \Longrightarrow \text { Matrix } P \text { is invertible. } \end{array}
We know that P^{-1}=\frac{\operatorname{adj}(P)}{\operatorname{det}(P)}
On solving, we get P^{-1}=\frac{1}{2}\left[\begin{array}{ccc} 0 & -2 & 1 \\ -2 & -2 & 0 \\ -2 & -2 & -1 \end{array}\right]
Putting in the Diagonalization of Matrix equation, we get
\begin{array}{l} \quad D=P^{-1} A P \\\\ D=\frac{1}{2}\left[\begin{array}{ccc} 0 & -2 & 1 \\ -2 & -2 & 0 \\ -2 & -2 & -1 \end{array}\right]\left[\begin{array}{ccc} 1 & 0 & -1 \\ 1 & 2 & 1 \\ 2 & 2 & 3 \end{array}\right]\left[\begin{array}{ccc} 1 & -2 & 1 \\ -1 & 1 & -1 \\ 0 & 2 & -2 \end{array}\right] \\\\ D=\left[\begin{array}{lll} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{array}\right] \end{array}
MATLAB Implementation for Diagonalization of a Square Matrix
Matlab
% MATLAB Implementation for
% Diagonalization of a Square Matrix:
clear all
clc
disp("MATLAB Implementation for Diagonalization
of a Square Matrix | GeeksforGeeks")
A = input("Enter a matrix A : ");
[P , D] = eig(A);
D1 = inv(P)*(A)*(P);
disp("Diagonal form 'D' of Input Matrix 'A' is:")
disp(D1)
Output:
For the Matrix: A = \begin{bmatrix} 1 & 3\\ 3 & 9\\ \end{bmatrix}
For the Matrix: A = \begin{bmatrix} 1 & 0 & -1 \\ 1 & 2 & 1 \\ 2 & 2& 3\\ \end{bmatrix}

The Diagonalization is Not Unique
If a matrix A is diagonalizable, there is no unique way to diagonalize it.
- The order of eigenvalues in the diagonal matrix D can be changed.
- A column in P can be replaced with a scalar multiple of itself, as it remains an eigenvector for the same eigenvalue.
- If an eigenvalue is repeated, different bases for its eigenspace can be chosen.
Example
in the previous example, we could have defined:
D = \begin{bmatrix} \lambda_2 & 0 & 0 \\ 0 & \lambda_1 & 0 \\ 0 & 0 & \lambda_3 \end{bmatrix} = \begin{bmatrix} 3 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 1 \end{bmatrix}
and
P = \begin{bmatrix} x_2 & x_1 & x_3 \end{bmatrix} = \begin{bmatrix} 1 & 0 & -1 \\ 1 & 2 & 1 \\ 2 & 2 & 3 \end{bmatrix}
Another possibility would have been to choose:
x_2 = \begin{bmatrix} -1 \\ -1 \\ -2 \end{bmatrix}
and
P = \begin{bmatrix} x_2 & x_1 & x_3 \end{bmatrix} = \begin{bmatrix} -1 & 0 & -1 \\ -1 & 2 & 1 \\ -2 & 2 & 3 \end{bmatrix}
This shows that diagonalization is not unique since we can change eigenvectors by scalar multiples or reorder them while maintaining the same eigenvalues.
Inverse Matrix
Once a matrix has been diagonalized, computing its inverse (if it exists) becomes straightforward.
In fact, we have:
A^{-1} = (PDP^{-1})^{-1} =P D^{-1} P^{-1}
where:
D^{-1} = \text{diag} \left(\frac{1}{\lambda_1}, \frac{1}{\lambda_2}, \dots, \frac{1}{\lambda_n} \right)
provided none of the eigenvalues λ are zero.
Practice Problems on Matrix Diagonalization
Problem 1: Check if a Matrix is Diagonalizable
Given the matrix: A = \begin{bmatrix} 4 & 1 \\ 6 & -1 \end{bmatrix}
Find the eigenvalues and eigenvectors. Determine if A is diagonalizable.
Problem 2: Compute the Diagonal Form
For the matrix: A = \begin{bmatrix} 2 & 0 & 0 \\ 1 & 3 & 1 \\ 0 & 0 & 3 \end{bmatrix}
Find the diagonal matrix D and the matrix P such that A= P D P−1.
Problem 3: Find the Inverse Using Diagonalization
Given the matrix: A = \begin{bmatrix} 1 & 2 \\ 4 & 3 \end{bmatrix}
Diagonalize A, and use it to compute A−1, if it exists.
Problem 4: Repeated Eigenvalues Case
Consider the matrix: A = \begin{bmatrix} 6 & -2 \\ 2 & 2 \end{bmatrix}
Check if it is diagonalizable, given that it has a repeated eigenvalue.
Problem 5: Diagonalizing a 3×3 Matrix
For the matrix: A = \begin{bmatrix} 7 & -4 & 4 \\ -4 & 8 & -4 \\ 4 & -4 & 7 \end{bmatrix}
Find the eigenvalues and eigenvectors. Determine P and D for diagonalization.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem