lettura simple

Replacing diagonals in a Matlab matrix

In this lesson, I will explain how to replace elements in a matrix diagonal using Matlab.

What are the diagonals of a matrix? They are the elements that are transversally located on diagonals that start from the top right and end at the bottom left or vice versa. For example, the main diagonal of matrix M is composed of the elements 1, 5, 9. $$ M = \begin{pmatrix} \color{red}1 & 2 & 3 \\ 4 & \color{red}5 & 6 \\ 7 & 8 & \color{red}9 \end{pmatrix} $$

Here is a simple practical example.

Create a 3x3 square matrix with three rows and three columns:

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

The main diagonal of the matrix is formed by the elements 1, 5, 9.

$$ M = \begin{pmatrix} \color{red}1 & 2 & 3 \\ 4 & \color{red}5 & 6 \\ 7 & 8 & \color{red}9 \end{pmatrix} $$

Type spdiags([-1;-5;-9],0,M) on the Matlab command line to replace the elements on the main diagonal with the numbers -1, -2, -3.

>> spdiags([-1;-5;-9],0,M)
ans =
-1 2 3
4 -5 6
7 8 -9

  • The first parameter of the command is a column vector [-1;-5;-9] containing the new elements to write on the matrix diagonal.
  • The second parameter (0) is the index of the diagonal where to insert them. The main diagonal has index 0.

    Note. Index 1 is the diagonal above the main diagonal, while index -1 is the diagonal below the main diagonal. Similarly, 2 and -2 are the diagonals above and below diagonals 1 and -1.

  • The third parameter M is the name of the variable where you saved the matrix.

This command modifies the square matrix by inserting the new elements on the main diagonal.

$$ M = \begin{pmatrix} \color{red}{-1} & 2 & 3 \\ 4 & \color{red}{-5} & 6 \\ 7 & 8 & \color{red}{-9} \end{pmatrix} $$

Now type the command spdiags([-1;-5;-9],1,M) to modify the elements on the diagonal above the main diagonal.

>> spdiags([-1;-5;-9],1,M)
ans =
1 -5 3
4 5 -9
7 8 9

This last command replaces the elements above the main diagonal of the matrix.

$$ M = \begin{pmatrix} 1 & \color{red}{-5} & 3 \\ 4 & 5 & \color{red}{-9} \\ 7 & 8 & 9 \end{pmatrix} $$

Note. The first element -1 of the column vector [-1;-5;-9] is not written in the new matrix because it is outside the matrix. Therefore, only -5 and -9 appear in the matrix.
why is element -1 not in array?

What about replacing the elements on the secondary diagonal?

The secondary diagonal starts at the top right and ends at the bottom left.

$$ M = \begin{pmatrix} 1 & 2 & \color{red}{3} \\ 4 & \color{red}{5} & 6 \\ \color{red}{7} & 8 & 9 \end{pmatrix} $$

To replace the elements that are on the secondary diagonal, you need to use the spdiags() and fliplr() functions.

>> fliplr(spdiags([-1;-5;-9],0,fliplr(M)))
ans =
1 2 -1
4 -5 6
-9 8 9

The fliplr() function flips the matrix from left to right.

$$ \text{ fliplr }(M) = \begin{pmatrix} 3 & 2 & 1 \\ 6 & 5 & 4 \\ 9 & 8 & 7 \end{pmatrix} $$

The spdiags() function replaces the values on the main diagonal of the flipped matrix.

$$ \text{ fliplr }(M) = \begin{pmatrix} \color{red}{-1} & 2 & 1 \\ 6 & \color{red}{-5} & 4 \\ 9 & 8 & \color{red}{-9} \end{pmatrix} $$

Therefore, on the original matrix M, you have replaced the elements on the secondary diagonal.

$$ M = \begin{pmatrix} 1 & 2 & \color{red}{-1} \\ 4 & \color{red}{-5} & 6 \\ \color{red}{-9} & 8 & 9 \end{pmatrix} $$

In this way, you can also replace the values on the other diagonals of the matrix.

Note. To modify the diagonals above and below the secondary diagonal, change the index of the spdiags() function to 1 or -1. For example, to modify the elements above the secondary diagonal, type the command
fliplr(spdiags([-1;-5;-9],1,fliplr(M)))
$$ M = \begin{pmatrix} 1 & \color{red}{-5} & 3 \\ \color{red}{-9} & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix} $$ Conversely, to modify the elements below the secondary diagonal, type the command
fliplr(spdiags([-1;-5;-9],-1,fliplr(M)))
$$ M = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & \color{red}{-1} \\ 7 & \color{red}{-5} & 9 \end{pmatrix} $$




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin