Matrix Calculator

🌐 English
A (3×3)
Rows: 3
Cols: 3
B (3×3)
Rows: 3
Cols: 3
Supports: fractions (1/2), decimals (0.5), constants (pi, e). Empty cells are treated as 0.

Calculation Results

1. Definition: What is a diagonal matrix?

A diagonal matrix is a square matrix in which all off-diagonal entries are zero. If $A=[a_{ij}]$ is an $n\times n$ matrix, then $A$ is diagonal when

$$a_{ij}=0\quad\text{for all }i\neq j$$

Equivalently, a diagonal matrix has the form:

$$D=\begin{bmatrix}d_1 & 0 & \cdots & 0\\[4pt]0 & d_2 & \cdots & 0\\[4pt]\vdots & \vdots & \ddots & \vdots\\[4pt]0 & 0 & \cdots & d_n\end{bmatrix}=\operatorname{diag}(d_1,d_2,\dots,d_n)$$

Diagonal matrices are easy to work with: addition, multiplication, powers and inversion (if no zero on the diagonal) are element-wise operations on the diagonal entries.

2. How to extract the diagonal matrix (formula)

Given a square matrix $A=[a_{ij}]$, the diagonal matrix formed from $A$ is

$$D=\operatorname{diag}(a_{11},a_{22},\dots,a_{nn})$$

In component form:

$$d_{ij}=\begin{cases}a_{ii}, & i=j\\[4pt]0, & i\neq j\end{cases}$$

3. Worked examples

Example 1

Given

$$A=\begin{bmatrix}4 & 8 & 1\\[4pt]2 & 5 & 7\\[4pt]9 & 3 & 6\end{bmatrix}$$

Diagonal extraction:

$$D=\operatorname{diag}(4,5,6)=\begin{bmatrix}4 & 0 & 0\\[4pt]0 & 5 & 0\\[4pt]0 & 0 & 6\end{bmatrix}$$

Example 2

Given

$$A=\begin{bmatrix}12 & -3 & 9\\[4pt]0 & 4 & 11\\[4pt]5 & 2 & -7\end{bmatrix}$$

Diagonal extraction:

$$D=\operatorname{diag}(12,4,-7)=\begin{bmatrix}12 & 0 & 0\\[4pt]0 & 4 & 0\\[4pt]0 & 0 & -7\end{bmatrix}$$

4. Common mistakes

  • Thinking diagonal means all diagonal entries equal: That's a scalar matrix; diagonal means off-diagonals are zero but diagonal entries can differ.
  • Confusing diagonal with diagonalizable: Diagonal is a matrix structure; diagonalizable is a property (similarity transform) of some matrices.
  • Failing to zero non-diagonal entries when extracting: Make sure to set every $i\neq j$ entry to zero.
  • Attempting to extract diagonal from a non-square matrix without clarifying: Extraction of diagonal is defined for square matrices; for rectangular matrices you can still form a diagonal matrix using the min(m,n) diagonal elements, but clarify intent.

5. Practice problems (answers hidden)

Click "Show Answer" to reveal each diagonal matrix.

Exercise 1

$$A=\begin{bmatrix}7 & 1 & -3\\[4pt]5 & 9 & 2\\[4pt]4 & 8 & 6\end{bmatrix}$$
Show Answer
$$\operatorname{diag}(7,9,6)=\begin{bmatrix}7 & 0 & 0\\[4pt]0 & 9 & 0\\[4pt]0 & 0 & 6\end{bmatrix}$$

Exercise 2

$$A=\begin{bmatrix}-2 & 4 & 7 & 1\\[4pt]3 & 5 & 0 & 9\\[4pt]8 & 6 & 10 & 2\\[4pt]1 & 3 & 4 & 11\end{bmatrix}$$
Show Answer
$$\operatorname{diag}(-2,5,10,11)=\begin{bmatrix}-2 & 0 & 0 & 0\\[4pt]0 & 5 & 0 & 0\\[4pt]0 & 0 & 10 & 0\\[4pt]0 & 0 & 0 & 11\end{bmatrix}$$

Exercise 3

$$A=\begin{bmatrix}1 & 9\\[4pt]-5 & 4\end{bmatrix}$$
Show Answer
$$\operatorname{diag}(1,4)=\begin{bmatrix}1 & 0\\[4pt]0 & 4\end{bmatrix}$$

Exercise 4

$$A=\begin{bmatrix}3 & -1 & 2\\[4pt]0 & 8 & -6\\[4pt]5 & 7 & 9\end{bmatrix}$$
Show Answer
$$\operatorname{diag}(3,8,9)=\begin{bmatrix}3 & 0 & 0\\[4pt]0 & 8 & 0\\[4pt]0 & 0 & 9\end{bmatrix}$$

Learn matrix multiplication more easily in just 2 minutes with this free game.

Matrix Multiplication Game