# diag(x)
diag(x=1, nrow, ncol)
diag(x) <- valuediag.spam(x=1, nrow, ncol)
diag.spam(x) <- value
spam
matrix, a vector or a scalar.x
is a spam matrix then diag(x)
returns the diagonal of x
. The assignment form sets the diagonal of the sparse matrix x
to the
given value(s).
diag.spam
works as diag
for spam matrices:
If x
is a vector (or 1D array) of length two or more, then
diag.spam(x)
returns a diagonal matrix whose diagonal is x
.
If x
is a vector of length one then diag.spam(x)
returns an
identity matrix of order the nearest integer to x
. The
dimension of the returned matrix can be specified by nrow
and
ncol
(the default is square).
The assignment form sets the diagonal of the matrix x
to the
given value(s).
diag(x)
can have unexpected effects if x
is a vector
that could be of length one. Use diag(x, nrow = length(x))
for
consistent behaviour.upper.tri
, lower.tri
.diag.spam(1, 4) # I4
smat <- diag.spam(1:5)
diag( smat)
diag( smat) <- 5:1
# The last line is equivalent to
diag.spam( smat) <- 5:1
# Note that diag.spam( 1:5) <- 5:1 not work of course.
Run the code above in your browser using DataLab