Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# diag

Get diagonal elements or create diagonal matrix

## Description

example

D = diag(v) returns a square diagonal matrix with the elements of vector v on the main (k=0) diagonal.

example

D = diag(v,k) places the elements of vector v on the kth diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.

example

x = diag(A) returns a column vector of the main (k=0) diagonal elements of A.

example

x = diag(A,k) returns a column vector of the elements on the kth diagonal of A.

## Examples

expand all

### Create Diagonal Matrices

Define a 1-by-5 vector.

`v = [2 1 -1 -2 -5];`

Use diag to create a matrix with the elements of v on the main diagonal.

`D = diag(v)`
```D =

2     0     0     0     0
0     1     0     0     0
0     0    -1     0     0
0     0     0    -2     0
0     0     0     0    -5```

The result is a 5-by-5 diagonal matrix.

Create a matrix with the elements of v on the first super diagonal (k=1).

`D1 = diag(v,1)`
```D1 =

0     2     0     0     0     0
0     0     1     0     0     0
0     0     0    -1     0     0
0     0     0     0    -2     0
0     0     0     0     0    -5
0     0     0     0     0     0```

The result is a 6-by-6 matrix. When you specify a vector of length n as an input, diag returns a square matrix of size n+abs(k).

### Get Diagonal Elements

Create a random 6-by-6 matrix.

```rng(0);
A = randi(10,6)```
```A =

9     3    10     8     7     8
10     6     5    10     8     1
2    10     9     7     8     3
10    10     2     1     4     1
7     2     5     9     7     1
1    10    10    10     2     9```

Get the elements on the main diagonal of A.

`x = diag(A)`
```x =

9
6
9
1
7
9```

The result is a column vector of the elements on the main diagonal of A.

Get the elements on the first sub diagonal (k=-1) of A.

`x1 = diag(A,-1)`
```x1 =

10
10
2
9
2```

The result has one less element than the main diagonal.

Nest calls to diag to generate a new diagonal matrix from the diagonal elements.

`A1 = diag(diag(A))`
```A1 =

9     0     0     0     0     0
0     6     0     0     0     0
0     0     9     0     0     0
0     0     0     1     0     0
0     0     0     0     7     0
0     0     0     0     0     9```

The result is a square diagonal matrix with the same main diagonal elements as A.

## Input Arguments

expand all

### v — Diagonal elementsvector

Diagonal elements, specified as a vector. If v is a vector with N elements, then diag(v,k) is a square matrix of order N+abs(k).

diag([]) returns an empty matrix, [].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

### A — Input matrixmatrix

Input matrix, specified as a matrix. diag returns an error if ndims(A) > 2.

diag([]) returns an empty matrix, [].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

### k — Diagonal numberinteger

Diagonal number, specified as an integer. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.

For an m-by-n matrix, k is in the range

## More About

expand all

### Tips

• When you specify a vector input, diag returns a square diagonal matrix. When you specify a matrix input, diag returns a column vector of diagonal elements.

• The trace of a matrix is equal to sum(diag(A)).

## See Also

Was this topic helpful?