\Header{diag}{Matrix Diagonals}
\alias{diag$\leftarrow $}{diag}
\keyword{array}{diag}
\begin{Usage}
\begin{verbatim}
diag(x, nrow, ncol)
diag(x) <- value
\end{verbatim}
\end{Usage}
\begin{Value}
If \texttt{x} is a matrix then \texttt{diag(x)} returns the diagonal of
\texttt{x}.  If \texttt{x} is a vector then \texttt{x} returns a diagonal
matrix whose diagonal is \texttt{x}.  If \texttt{x} is an integer then
\texttt{diag(x)} returns an identity matrix of order \texttt{x}.
The dimension of the returned matrix can be specified by
\texttt{nrow} and \texttt{ncol} (the default is square).

The assignment form sets the diagonal of the matrix \texttt{x} to the
given value(s).\end{Value}
\begin{SeeAlso}
\texttt{\Link{matrix}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
dim(diag(3))
diag(10,3,4) # guess what?
all(diag(1:3) == {m <- matrix(0,3,3); diag(m) <- 1:3; m})
\end{ExampleCode}
\end{Examples}

