# Create Diagonal Matrix using Python

*This article was first published on*

Want to share your content on python-bloggers? click here.

**PyShark**, and kindly contributed to python-bloggers. (You can report issue about the content on this page here)Want to share your content on python-bloggers? click here.

In this article we will discuss the steps and intuition for creating the diagonal matrix and show examples using Python.

**Table of contents**

- Introduction
- Diagonal matrix explained
- Create diagonal matrix using Python
- Extract diagonal from matrix using Python
- Conclusion

**Books I recommend:**

- Python Crash Course
- Automate the Boring Stuff with Python
- Beyond the Basic Stuff with Python
- Serious Python

## Introduction

The diagonal matrix (\(D\)) is most often seen in linear algebra expressions that involve the identity matrices.

To continue following this tutorial we will need the following Python library: numpy.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:

pip install numpy

## Diagonal matrix explained

We are already familiar with the identity matrix and some of its properties, and it actually is a special case of a diagonal matrix.

A diagonal matrix is a matrix (usually a square matrix of order \(n\)) filled with values on the main diagonal and zeros everywhere else.

Here are a few examples:

$$D_1 = \begin{bmatrix} 3 \end{bmatrix}$$

$$D_2 = \begin{bmatrix} 3 & 0 \\ 0 & 2 \end{bmatrix}$$

$$D_3 = \begin{bmatrix} 3 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 5 \end{bmatrix}$$

and so on for the larger dimensions.

Graphically, the \(D_2\) matrix simply represents the scaled base vectors:

$$\vec{d}_1 = (3, 0)$$

$$\vec{d}_2 = (0, 2)$$

There are usually two main use cases for the diagonal matrix:

- Creating a diagonal matrix – implies taking a vector and creating a matrix where the values from a vector become the main diagonal of a matrix.
- Extracting a diagonal from a matrix – implies finding the main diagonal in a given matrix.

## Create diagonal matrix using Python

In order to create a diagonal matrix using Python we will use the numpy library. And the first step will be to import it:

import numpy as np

Numpy has a lot of useful functions, and for this operation we will use the diag() function. This function is particularly interesting, because if we pass a 1-D array into it, it will return a 2-D array (or a matrix) with the vector values on its \(k\)-th diagonal (\(k\)=0 for the main diagonal).

Now let’s create some 1-D array (or a vector):

v = [3, 2, 5]

and create a matrix with \(v\) on the main diagonal:

D = np.diag(v) print(D)

And you should get:

[[3 0 0] [0 2 0] [0 0 5]]

which is a diagonal matrix with values on the main diagonal and zeros everywhere else.

## Extract diagonal from matrix using Python

In order to extract a diagonal from a matrix using Python we will use the numpy library. And the first step will be to import it:

import numpy as np

Numpy has a lot of useful functions, and for this operation we will use the diag() function. This function is particularly interesting, because if we pass a 2-D array into it, it will return its \(k\)-th diagonal (\(k\)=0 for the main diagonal).

Now let’s create some 3×3 matrix:

A = np.array([[3, 1, 4], [0, 2, 7], [8, 9, 5]])

and extract it’s main diagonal:

d = np.diag(A) print(d)

And you should get:

[3 2 5]

which are exactly the values on the main diagonal of the matrix.

## Conclusion

In this article we discussed the steps and intuition for creating the diagonal matrix, as well as extracting a diagonal from a matrix using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.

The post Create Diagonal Matrix using Python appeared first on PyShark.

**leave a comment**for the author, please follow the link and comment on their blog:

**PyShark**.

Want to share your content on python-bloggers? click here.