diagΒΆ

diag creates a diagonal-matrix or a row-vector.

r = diag(m)
r = diag(m, k)

Return Value

If m is a row- or column-vector, diag creates a matrix. If m is a nr * nc matrix, diag creates a row-vector with min([nr, nc]) columns.

Parameters

m

m is a real, complex or string vector or matrix.

k

k is a real scalar and sets the of kth diagonal: k = 0 represents the main diagonal, k > 0 above the main diagonal, and k < 0 below the main diagonal. Default value is 0.

Example

diag([1,2,3])
    1.0000     0.0000     0.0000
    0.0000     2.0000     0.0000
    0.0000     0.0000     3.0000
[1,2;3,4]
    1.0000     2.0000
    3.0000     4.0000
diag([1,2;3,4])
    1.0000     4.0000

diag([1,2,3], 2)
    0.0000     0.0000     1.0000     0.0000     0.0000
    0.0000     0.0000     0.0000     2.0000     0.0000
    0.0000     0.0000     0.0000     0.0000     3.0000
    0.0000     0.0000     0.0000     0.0000     0.0000
    0.0000     0.0000     0.0000     0.0000     0.0000

History

Version

Description

R2013.2

New parameter k.

id-1146555