MATLAB diagonal matrix

diag() command or function gives diagonal elements or creates a diagonal matrix, after reading this MATLAB diagonal matrix topic, you will know the theory and examples.


Syntax:

diag(n)

  • n can be a vector or matrix.

Example: To create a 2×2 diagonal matrix having diagonal elements as 1 and 2.

Output:

ans =

1 0
0 2

Example: To create a column vector of diagonal elements from the given Matrix.

Output:

n =

     1     2     3
     3     2     1
     4     5     1


v =

     1
     2
     1

Leave a Comment

error: Content is protected !!