[R] creating a symmetric matrix

Bert Gunter gunter.berton at gene.com
Fri Sep 7 18:32:13 CEST 2012


???
A previous answer pointed out ?toeplitz

entering

> toeplitz

gives

{
    if (!is.vector(x))
        stop("'x' is not a vector")
    n <- length(x)
    A <- matrix(0, n, n)
    matrix(x[abs(col(A) - row(A)) + 1], n, n)
}

Why reinvent?

-- Bert


On Fri, Sep 7, 2012 at 8:19 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> You asked for existing functions, but I was more intrigued by trying to
> create one:
>
> symmat <- function(n) {
>      x <- matrix(1:n, nrow=n, ncol=n)
>      abs(x-col(x))+1
> }
>
>> symmat(4)
>      [,1] [,2] [,3] [,4]
> [1,]    1    2    3    4
> [2,]    2    1    2    3
> [3,]    3    2    1    2
> [4,]    4    3    2    1
>
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Erin Hodgess
>> Sent: Thursday, September 06, 2012 8:11 PM
>> To: R help
>> Subject: [R] creating a symmetric matrix
>>
>> Dear R People:
>>
>> Suppose I have the following:
>>
>> x <- 1:4
>>
>> And I want to create the following matrix:
>>
>> 1 2 3 4
>> 2 1 2 3
>> 3 2 1 2
>> 4 3 2 1
>>
>> Is there a function in place for this, please?  I looked at  symMatrix
>> in micEcon and some of the items in Matrix, but they didn't quite do
>> it.
>>
>> I can put together something quickly, of course, but it seems likely
>> that this would exist.
>>
>> Thanks,
>> Erin
>>
>>
>> --
>> Erin Hodgess
>> Associate Professor
>> Department of Computer and Mathematical Sciences
>> University of Houston - Downtown
>> mailto: erinm.hodgess at gmail.com
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm




More information about the R-help mailing list