EVOLUTION-MANAGER
Edit File: diag.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Matrix Diagonals</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for diag {base}"><tr><td>diag {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Matrix Diagonals</h2> <h3>Description</h3> <p>Extract or replace the diagonal of a matrix, or construct a diagonal matrix. </p> <h3>Usage</h3> <pre> diag(x = 1, nrow, ncol, names = TRUE) diag(x) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a matrix, vector or 1D <code><a href="array.html">array</a></code>, or missing.</p> </td></tr> <tr valign="top"><td><code>nrow, ncol</code></td> <td> <p>optional dimensions for the result when <code>x</code> is not a matrix.</p> </td></tr> <tr valign="top"><td><code>names</code></td> <td> <p>(when <code>x</code> is a matrix) logical indicating if the resulting vector, the diagonal of <code>x</code>, should inherit <code><a href="names.html">names</a></code> from <code>dimnames(x)</code> if available.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>either a single value or a vector of length equal to that of the current diagonal. Should be of a mode which can be coerced to that of <code>x</code>.</p> </td></tr> </table> <h3>Details</h3> <p><code>diag</code> has four distinct usages: </p> <ol> <li> <p><code>x</code> is a matrix, when it extracts the diagonal. </p> </li> <li> <p><code>x</code> is missing and <code>nrow</code> is specified, it returns an identity matrix. </p> </li> <li> <p><code>x</code> is a scalar (length-one vector) and the only argument, it returns a square identity matrix of size given by the scalar. </p> </li> <li> <p><code>x</code> is a ‘numeric’ (<code><a href="complex.html">complex</a></code>, <code>numeric</code>, <code>integer</code>, <code><a href="logical.html">logical</a></code>, or <code><a href="raw.html">raw</a></code>) vector, either of length at least 2 or there were further arguments. This returns a matrix with the given diagonal and zero off-diagonal entries. </p> </li></ol> <p>It is an error to specify <code>nrow</code> or <code>ncol</code> in the first case. </p> <h3>Value</h3> <p>If <code>x</code> is a matrix then <code>diag(x)</code> returns the diagonal of <code>x</code>. The resulting vector will have <code><a href="names.html">names</a></code> if the matrix <code>x</code> has matching column and rownames. </p> <p>The replacement form sets the diagonal of the matrix <code>x</code> to the given value(s). </p> <p>In all other cases the value is a diagonal matrix with <code>nrow</code> rows and <code>ncol</code> columns (if <code>ncol</code> is not given the matrix is square). Here <code>nrow</code> is taken from the argument if specified, otherwise inferred from <code>x</code>: if that is a vector (or 1D array) of length two or more, then its length is the number of rows, but if it is of length one and neither <code>nrow</code> nor <code>ncol</code> is specified, <code>nrow = as.integer(x)</code>. </p> <p>When a diagonal matrix is returned, the diagonal elements are one except in the fourth case, when <code>x</code> gives the diagonal elements: it will be recycled or truncated as needed, but fractional recycling and truncation will give a warning. </p> <h3>Note</h3> <p>Using <code>diag(x)</code> can have unexpected effects if <code>x</code> is a vector that could be of length one. Use <code>diag(x, nrow = length(x))</code> for consistent behaviour. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="lower.tri.html">upper.tri</a></code>, <code><a href="lower.tri.html">lower.tri</a></code>, <code><a href="matrix.html">matrix</a></code>. </p> <h3>Examples</h3> <pre> dim(diag(3)) diag(10, 3, 4) # guess what? all(diag(1:3) == {m <- matrix(0,3,3); diag(m) <- 1:3; m}) ## other "numeric"-like diagonal matrices : diag(c(1i,2i)) # complex diag(TRUE, 3) # logical diag(as.raw(1:3)) # raw (D2 <- diag(2:1, 4)); typeof(D2) # "integer" require(stats) ## diag(<var-cov-matrix>) = variances diag(var(M <- cbind(X = 1:5, Y = rnorm(5)))) #-> vector with names "X" and "Y" rownames(M) <- c(colnames(M), rep("", 3)) M; diag(M) # named as well diag(M, names = FALSE) # w/o names </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>