diag_matlab: Function written to match MATLAB's diag function

Description Usage Arguments Value See Also Examples

View source: R/diag_matlab.R

Description

There are some differences between tha MATLAB and the R version of diag. Specifically, if a 1xN or a Nx1 matrix is supplied to the R diag function then just the first element of this vector is returned. This function tries to match the MATLAB version in handling vectors (matricies with one dimension equal to one), and will return a diagonal matrix in these situations.

Usage

1

Arguments

mat

Either a vector to make into a diagonal matrix or a matrix you want to extract the diagonal from

Value

Either a diagonal matrix or the diagonal of a matrix.

See Also

Other MATLAB: cell(), feval(), fileparts(), isempty(), ones(), randn(), rand(), size(), tic(), toc(), zeros()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diag_matlab(3)
diag_matlab(c(1,2,3))
diag_matlab(cbind(1,2,3))
diag_matlab(rbind(1,2,3))

diag_matlab(matrix(c(1, 2, 3),6,6))

# here is where the R default does something different
diag(cbind(1,2,3))
diag(rbind(1,2,3))

PopED documentation built on May 21, 2021, 5:08 p.m.