Skip to contents

tda_mdiag builds an n x n diagonal matrix from a length-n vector – TDA's mdiag, matching base R's diag(x) when x has length > 1. tda_mdiagd extracts the diagonal of a matrix as a plain vector, using min(nrow(A), ncol(A)) entries for a non-square A – TDA's mdiagd, matching base R's diag(A).

Usage

tda_mdiag(x, dir = tempfile("tda"))

tda_mdiagd(A, dir = tempfile("tda"))

Arguments

x

a numeric vector.

dir

working directory.

A

a numeric matrix.

Value

tda_mdiag returns a matrix; tda_mdiagd returns a plain numeric vector.

Examples

tda_mdiag(c(5, 7, 9))
#>      [,1] [,2] [,3]
#> [1,]    5    0    0
#> [2,]    0    7    0
#> [3,]    0    0    9
tda_mdiagd(matrix(1:6, 2, 3))
#> [1] 1 4