Re: [R] Diagonal matrix with off diagonal elements

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat, 22 Dec 2007 11:29:30 +0100

Chris Stubben wrote:
> Also try the odiag function in the demogR package
>
> odiag( 1:5, -1)
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 0 0 0 0 0 0
> [2,] 1 0 0 0 0 0
> [3,] 0 2 0 0 0 0
> [4,] 0 0 3 0 0 0
> [5,] 0 0 0 4 0 0
> [6,] 0 0 0 0 5 0
>
> Chris
>
>
Also, this sort of pattern works

 > m <- matrix(0,6,6)
 > diag(m[-1,])<-1:5
 > m
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    0    0    0    0    0
[2,]    1    0    0    0    0    0
[3,]    0    2    0    0    0    0
[4,]    0    0    3    0    0    0

[5,] 0 0 0 4 0 0
[6,] 0 0 0 0 5 0

>
>
>
> Jonas Malmros wrote:
>
>> Hi, everyone
>>
>> I wonder if there is a function in R with which I can create a square
>> matrix with elements off main diagonal (for example one diagonal below
>> the main diagonal).
>>
>> Thanks in advance!
>>
>> --
>> Jonas Malmros
>> Stockholm University
>> Stockholm, Sweden
>>
>> ______________________________________________
>> 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.
>>
>>
>>
>
>

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
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.
Received on Sat 22 Dec 2007 - 10:32:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 24 Dec 2007 - 14:00:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.