Dear R users, The solution is probably simple but I need someone to point me to it. How can I to generate a matrix from a numeric sequence of 1:10 like 'A' or 'B' below: A) |--------------------| | 1 2 3 4 5 | |--------------------| | 1 | 0 | | 2 | 1 0 | | 3 | 2 5 0 | | 4 | 3 6 8 0 | | 5 | 4 7 9 10 0 | |--------------------| B) |--------------------| | 1 2 3 4 5 | |--------------------| | 1 | 0 1 2 3 4 | | 2 | 1 0 5 6 7 | | 3 | 2 5 0 8 9 | | 4 | 3 6 8 0 10 | | 5 | 4 7 9 10 0 | |--------------------| This question is related with the possible combinations of five objects two the two, i.e, C(5,2). Any help would be greatly appreciated. Regards, -- Jose Claudio Faria Brasil/Bahia/UESC/DCET Estatistica Experimental/Prof. Adjunto mails: joseclaudio.faria at terra.com.br jc_faria at uesc.br jc_faria at uol.com.br tel: 73-3634.2779
On 7/9/05, Jose Claudio Faria <joseclaudio.faria at terra.com.br> wrote:> Dear R users, > > The solution is probably simple but I need someone to point me to it. > How can I to generate a matrix from a numeric sequence of 1:10 like 'A' or 'B' > below: > > A) > |--------------------| > | 1 2 3 4 5 | > |--------------------| > | 1 | 0 | > | 2 | 1 0 | > | 3 | 2 5 0 | > | 4 | 3 6 8 0 | > | 5 | 4 7 9 10 0 | > |--------------------| > > B) > |--------------------| > | 1 2 3 4 5 | > |--------------------| > | 1 | 0 1 2 3 4 | > | 2 | 1 0 5 6 7 | > | 3 | 2 5 0 8 9 | > | 4 | 3 6 8 0 10 | > | 5 | 4 7 9 10 0 | > |--------------------| >Try this and see ?lower.tri A <- matrix(0,5,5) A[lower.tri(A)] <- 1:10 B <- A + t(A)
In addition to Gabor's solution, you might be interested in the combinations function from the gtools package. library(gtools) combinations(5,2) [,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 1 4 [4,] 1 5 [5,] 2 3 [6,] 2 4 [7,] 2 5 [8,] 3 4 [9,] 3 5 [10,] 4 5 On Sat, 2005-07-09 at 21:42 -0300, Jose Claudio Faria wrote:> Dear R users, > > The solution is probably simple but I need someone to point me to it. > How can I to generate a matrix from a numeric sequence of 1:10 like 'A' or 'B' > below: > > A) > |--------------------| > | 1 2 3 4 5 | > |--------------------| > | 1 | 0 | > | 2 | 1 0 | > | 3 | 2 5 0 | > | 4 | 3 6 8 0 | > | 5 | 4 7 9 10 0 | > |--------------------| > > B) > |--------------------| > | 1 2 3 4 5 | > |--------------------| > | 1 | 0 1 2 3 4 | > | 2 | 1 0 5 6 7 | > | 3 | 2 5 0 8 9 | > | 4 | 3 6 8 0 10 | > | 5 | 4 7 9 10 0 | > |--------------------| > > This question is related with the possible combinations of five objects two the > two, i.e, C(5,2). > > Any help would be greatly appreciated. > > Regards,