[R] Matrix Construction; Subdiagonal

Sundar Dorai-Raj sdorairaj at gmail.com
Thu Mar 12 00:49:11 CET 2009


Does this help?

A <- matrix(0, 6, 6)
vec <- 1:5
A[row(A) == col(A) + 1] <- vec

--sundar

On Wed, Mar 11, 2009 at 4:42 PM, Stu Field <sgf at colostate.edu> wrote:
> I'm trying to enter a vector into the subdiagonal of a matrix but
> cannot find a command in R which corresponds to the MatLab version of
> diag(vec, k), where vec = the vector of interest, and k = the diagonal
> (k=0 for the diagonal; k=-1 for the subdiagonal; k=1 for
> superdiagonal, etc.)
> Is there an equivalent command in R?
>
> I'm looking for something like this:
> vec = seq(1, 5, 1)        # vector of interest
>
> A = xyz(vec,-1)           # creates a 6x6 matrix with vec on the
> subdiagonal
> where xyz is some function similar to diag, but with differing
> arguments.
>
> I can't believe there is not a simple way to do this...
> Thanks for your help,
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Stu Field, PhD
> Postdoctoral Fellow
> Department of Biology
> Colorado State University
> 1878 Campus Delivery
> Fort Collins, CO 80523-1878
> Office: E208 Anatomy/Zoology
> Phone: (970) 491-5744
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list