Just Learn Code

Mastering Diagonal Matrices in MATLAB: Creation and Modification Techniques

Matrices are a fundamental concept in linear algebra, and they are commonly used in engineering, physics, and computer science. Specifically, diagonal matrices hold a significant place in mathematics due to their unique properties.

Diagonal matrices are matrices where all non-diagonal elements are zero. Diagonal matrices may be of square size with equal numbers of rows and columns or rectangular size with varying number of rows and columns.

This article will cover the creation of diagonal matrices using the diag() and spdiags() functions in MATLAB. We will explore how to create a square diagonal matrix and how to modify diagonal elements, as well as extract diagonal entries from matrices.

Next, we will look at how to construct a tridiagonal matrix and how to modify entries on its main diagonal. Finally, this article will demonstrate how to extract non-zero diagonal entries from matrices using MATLAB.

Making Diagonal Matrix Using diag() Function in MATLAB:

The diag() function is a simple approach to create a diagonal matrix in MATLAB. The function can generate diagonal matrices from vectors or square matrices.

Diagonal matrices equipped with matrix multiplication have various uses in electronic circuit design, signal processing, and data analysis. Creating a Square Diagonal Matrix:

The simplest form of the diag() function creates a square diagonal matrix that includes the input vector elements on its main diagonal.

Consider the following code for a vector, v, that introduces random natural numbers as a basis for the matrix:


v = [3 6 5 2];

result = diag(v)


In the code example, we assigned the values [3 6 5 2] to the vector v. Correspondingly, the diag() function utilizes the vector to create a 4×4 square diagonal matrix containing the given values.

The output of the code is:


result =

3 0 0 0

0 6 0 0

0 0 5 0

0 0 0 2


It is important to note that assigning more elements to the input vector results in a larger square diagonal matrix. On the other hand, using fewer elements assigns zero values to elements not included in the vector.

Changing Position of Diagonal:

The diag() function supports more than one form of input arguments. Another option that is less commonly used is inputting the matrix elements directly as a function argument.

The assignation of elements in this form allows for customization, such as changing the diagonal position of the matrix. “`

result = diag(1:4, -1);


In this example, the diag() function generates a 5×5 square diagonal matrix with the diagonal one place below the main diagonal.

The function argument `-1` represents the desired position of the diagonal. The output of the code is:


result =

0 0 0 0 0

1 0 0 0 0

0 2 0 0 0

0 0 3 0 0

0 0 0 4 0


Extracting Diagonal Entries from a Matrix:

The MATLAB function diag() also supports extracting the diagonal elements of an existing matrix.

The diag() function extracts the column vector of elements corresponding to the diagonal of a matrix. “`

A = [1 2 3; 4 5 6; 7 8 9];

result = diag(A)


Here, we assigned the values [1 2 3; 4 5 6; 7 8 9] to the matrix A.

Accordingly, the diag() function extracts the diagonal elements, which are [1; 5; 9]. The output of the code is:


result =





Making Diagonal Matrix Using spdiags() Function in MATLAB:

The spdiag() function is an alternative to the diag() function generated specifically for constructing tridiagonal matrices.

Tridiagonal matrices represent matrices with zero elements except in the main diagonal and diagonal adjacent to it. Creating a Tridiagonal Matrix:

The spdiags() function produces a tridiagonal matrix in MATLAB.

This function takes three primary arguments: the central diagonal of the matrix, position of diagonal elements, and the matrix’s original size. “`

B = spdiags([1,2,3]’, 0:2, 3, 3)


Here, the array [1,2,3] is assigned to the central diagonal of the matrix.

The second argument, [0:2], determines the position for the diagonal. The last two arguments represent the resulting matrix’s size.

As a result, the code generates a 3×3 tridiagonal matrix containing the central diagonal [1 2 3], followed by secondary diagonals in adjacent positions. The output of the code is:


B =

1 2 0

0 2 3

0 0 3


Changing Diagonal Values of a Matrix:

Similar to the diag() function, the spdiags() function allows changing the values of the diagonal elements.


C = spdiags([4; 2; 1; 3], 0, 4, 4)


In this code, we assigned the vector [4 2 1 3] to the central diagonal of the tridiagonal matrix, which represented the main diagonal of matrix C. The diagonals’ positions are represented by the argument value 0.

Matrix C becomes a square with a dimension of 4×4. The output will be:


C =

4 0 0 0

0 2 0 0

0 0 1 0

0 0 0 3


Extracting Non-Zero Diagonal Entries from a Matrix:

If a matrix is not a well-ordered diagonal matrix, we may use the spdiags() function to extract the non-zero diagonal elements.


D = [1 2 0; 0 3 4; 5 0 6]

result = spdiags(D)’


In this code, we create D, a 3×3 matrix with non-zero diagonal values (1, 3, 6). We then apply the spdiags() function to D and transpose it, indicating the desired output is a row vector.

The output of the code is:


result =

1 3 6



Diagonal matrices are advantageous and versatile. This article covers the steps to create diagonal matrices and change the position of diagonal elements using diag() and spdiags() functions in MATLAB.

Extracting diagonal values as well as producing tridiagonal matrices and extracting non-zero diagonal entries from a matrix has also been presented. These topics covered are crucial techniques that are used in scientific computations and related fields.

We hope that by reading through this guide, you will be able to master creating and modifying diagonal matrices for any application. In summary, this article aimed to educate readers on creating diagonal matrices using MATLAB’s diag() and spdiags() functions.

We have explored various techniques for creating, modifying, and extracting diagonal elements from matrices. Diagonal matrices can be useful in many scientific and engineering applications, and understanding how to work with them is essential in scientific computation.

As a final thought, we should recall that diagonal matrices simplify the matrix operation, which improves the overall efficiency of scientific computation.

Popular Posts