s-news
[Top] [All Lists]

Re: Additional summary to sum [i,j] and [j,i] in a matrix efficiently.

To: <s-news@lists.biostat.wustl.edu>
Subject: Re: Additional summary to sum [i,j] and [j,i] in a matrix efficiently.
From: "Steve Su" <s.su@qut.edu.au>
Date: Tue, 17 Jun 2003 13:11:07 +1000
References: <5.2.1.1.0.20030615103644.02f27bb8@mail.attbi.com>
Dear All,

I would like to thank Greg Rodd, Spencer Graves, and Patrick Burns for your
help.

Many answer to my question were possible, other than those not posted to the
web:

h <- g - g*lower.tri(g) + t(g*lower.tri(g))

# Or:

h<-g+t(g)-diag(c(diag(g)))
h[lower.tri(h)]<-0

Steve.





> > > g
> >         [,1]   [,2]  [,3]  [,4]
> >[1,] 0.08 0.06 0.02 0.04
> >[2,] 0.12 0.09 0.03 0.06
> >[3,] 0.04 0.03 0.01 0.02
> >[4,] 0.16 0.12 0.04 0.08
> >
> >Now from g I want to obtain the following matrix:
> >
> >0.08     0.18     0.06     0.2
> >0          0.09     0.06     0.18
> >0          0          0.01     0.06
> >0          0          0          0.08
> >
> >This is obtained by assigning
> >g[i,j]<-g[i,j]+g[j,i] for i<j
> >g[j,i]<-0 for i>j.
> >
> >Is there a way to get the above matrix other than using a loop for this?
My
> >solution works like this:
> >
> >g[1,2:4]<-g[1,2:4]+g[2:4,1]
> >g[2:4,1]<-0
> >
> >g[2,3:4]<-g[2,3:4]+g[3:4,2]
> >g[3:4,2]<-0
> >
> >and so forth.
> >
> >fun.add.mat2<-function(x){
> >k<-ncol(x)
> >for(i in 1:(k-1)){
> >x[i,(i+1):k]<-x[i,(i+1):k]+x[(i+1):k,i]
> >x[(i+1):k,i]<-0}
> >return(x)
> >}
> >
> >fun.add.mat2(x=outer(a,b))
> >
> >Any suggestions welcome.
> >
> >
>
>***************************************************************************
*
> >**********
> >
> >  Steve Su (s.su@qut.edu.au)
> >  PhD student.
> >
> >  School of Accountancy
> >  School of Mathematical Sciences
> >  Queensland University of Technology
> >
> >  Postal Address: Steve Su, School of Accountancy, QUT, PO Box 2434,
> >Brisbane,
> >  Queensland, Australia, 4000.
> >
> >
> >  Phone:  +61 7 3864 2017
> >  Fax:    +61 7 3864 1812
> >  Mobile: 0421  840  586
> >      .
> >    _--_|\
> >   /      QUT
> >   \_.--._/
> >         v
> >
>
>***************************************************************************
*
> >**********
> >
> >--------------------------------------------------------------------
> >This message was distributed by s-news@lists.biostat.wustl.edu.  To
> >unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> >the BODY of the message:  unsubscribe s-news
>
> Gregory D. Rodd  303-641-6732      1-309-285-5836 (fax)
> Altalytics, LLC  P.O. Box 19981    Boulder CO 80308-2981
> datavizier@attbi.com
>
>
>
>


<Prev in Thread] Current Thread [Next in Thread>
  • Re: Additional summary to sum [i,j] and [j,i] in a matrix efficiently., Steve Su <=