Triangular matrix upper to down

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Triangular matrix upper to down

m.marcinmichal
Hi,
suppose that we have a triangular upper matrix A

test <- matrix(ncol = 4, nrow = 4)
test[1, ] <-  c(NA,1,1,1)
test[2, ] <-  c(NA,NA,1,1)
test[3, ] <-  c(NA,NA,NA,1)
test[4, ] <-  c(NA,NA,NA,NA)

I know how quickly set diagonal value diag(test) <- 1. But how quickly set down value i.e. matrix is symmetrical? Is there in r project any quickly function?

Thanks,

Best

Marcin
Reply | Threaded
Open this post in threaded view
|

Re: Triangular matrix upper to down

Michael Weylandt
On Mon, Sep 26, 2011 at 4:09 PM, R. Michael Weylandt <
[hidden email]> wrote:

> How about: test[lower.tri(test)] <- test[upper.tri(test)]
>
> Try it with this data so you can see that it actually works: (the ones
> obscure possible false solutions)
>
>
> test <- matrix(ncol = 4, nrow = 4)
>  test[1, ] <-  c(NA,1,2,3)
> test[2, ] <-  c(NA,NA,4,5)
> test[3, ] <-  c(NA,NA,NA,6)
>
> test[4, ] <-  c(NA,NA,NA,NA)
>
> print(test)
> test[lower.tri(test)] <- test[upper.tri(test)]
> print(test)
>
> Hope this helps,
>
> Michael Weylandt
>
>
>
> On Mon, Sep 26, 2011 at 3:50 PM, m.marcinmichal <[hidden email]>wrote:
>
>> Hi,
>> suppose that we have a triangular upper matrix A
>>
>> test <- matrix(ncol = 4, nrow = 4)
>> test[1, ] <-  c(NA,1,1,1)
>> test[2, ] <-  c(NA,NA,1,1)
>> test[3, ] <-  c(NA,NA,NA,1)
>> test[4, ] <-  c(NA,NA,NA,NA)
>>
>> I know how quickly set diagonal value diag(test) <- 1. But how quickly set
>> down value i.e. matrix is symmetrical? Is there in r project any quickly
>> function?
>>
>> Thanks,
>>
>> Best
>>
>> Marcin
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Triangular-matrix-upper-to-down-tp3845107p3845107.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Triangular matrix upper to down

Michael Weylandt
Nope, I was sloppy and missed that. Thanks

... forwarding to list and OP

Michael

On Mon, Sep 26, 2011 at 4:26 PM, William Dunlap <[hidden email]> wrote:

> My r-help mail is arriving out of order, so perhaps
> you have already corrected this, but you need a call
> to t() to make this work.  Your solution does not
> give a symmetric matrix (which I think the OP wants):
>   > test[lower.tri(test)] <- test[upper.tri(test)]
>  > print(test)
>        [,1] [,2] [,3] [,4]
>  [1,]   NA    1    2    3
>  [2,]    1   NA    4    5
>  [3,]    2    3   NA    6
>  [4,]    4    5    6   NA
> but the following does
>  > test[lower.tri(test)] <- t(test)[upper.tri(test)]
>  > print(test)
>       [,1] [,2] [,3] [,4]
>  [1,]   NA    1    2    3
>  [2,]    1   NA    4    5
>  [3,]    2    4   NA    6
>  [4,]    3    5    6   NA
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> On Behalf Of R. Michael
> > Weylandt
> > Sent: Monday, September 26, 2011 1:10 PM
> > To: m.marcinmichal; r-help
> > Subject: Re: [R] Triangular matrix upper to down
> >
> > On Mon, Sep 26, 2011 at 4:09 PM, R. Michael Weylandt <
> > [hidden email]> wrote:
> >
> > > How about: test[lower.tri(test)] <- test[upper.tri(test)]
> > >
> > > Try it with this data so you can see that it actually works: (the ones
> > > obscure possible false solutions)
> > >
> > >
> > > test <- matrix(ncol = 4, nrow = 4)
> > >  test[1, ] <-  c(NA,1,2,3)
> > > test[2, ] <-  c(NA,NA,4,5)
> > > test[3, ] <-  c(NA,NA,NA,6)
> > >
> > > test[4, ] <-  c(NA,NA,NA,NA)
> > >
> > > print(test)
> > > test[lower.tri(test)] <- test[upper.tri(test)]
> > > print(test)
> > >
> > > Hope this helps,
> > >
> > > Michael Weylandt
> > >
> > >
> > >
> > > On Mon, Sep 26, 2011 at 3:50 PM, m.marcinmichal <
> [hidden email]>wrote:
> > >
> > >> Hi,
> > >> suppose that we have a triangular upper matrix A
> > >>
> > >> test <- matrix(ncol = 4, nrow = 4)
> > >> test[1, ] <-  c(NA,1,1,1)
> > >> test[2, ] <-  c(NA,NA,1,1)
> > >> test[3, ] <-  c(NA,NA,NA,1)
> > >> test[4, ] <-  c(NA,NA,NA,NA)
> > >>
> > >> I know how quickly set diagonal value diag(test) <- 1. But how quickly
> set
> > >> down value i.e. matrix is symmetrical? Is there in r project any
> quickly
> > >> function?
> > >>
> > >> Thanks,
> > >>
> > >> Best
> > >>
> > >> Marcin
> > >>
> > >> --
> > >> View this message in context:
> > >>
> http://r.789695.n4.nabble.com/Triangular-matrix-upper-to-down-tp3845107p3845107.html
> > >> Sent from the R help mailing list archive at Nabble.com.
> > >>
> > >> ______________________________________________
> > >> [hidden email] 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.
> > >>
> > >
> > >
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Triangular matrix upper to down

William Dunlap
And, of course, I messed up that example as well by
not reinitializing the matrix.  It should be
> test[lower.tri(test)] <- NA
> test
     [,1] [,2] [,3] [,4]
[1,]   NA    1    2    3
[2,]   NA   NA    4    5
[3,]   NA   NA   NA    6
[4,]   NA   NA   NA   NA
> test[lower.tri(test)] <- t(test)[lower.tri(test)] # a correct solution
> test
     [,1] [,2] [,3] [,4]
[1,]   NA    1    2    3
[2,]    1   NA    4    5
[3,]    2    4   NA    6
[4,]    3    5    6   NA

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
From: R. Michael Weylandt [mailto:[hidden email]]
Sent: Monday, September 26, 2011 1:30 PM
To: William Dunlap; r-help; m.marcinmichal
Subject: Re: [R] Triangular matrix upper to down

Nope, I was sloppy and missed that. Thanks

... forwarding to list and OP

Michael
On Mon, Sep 26, 2011 at 4:26 PM, William Dunlap <[hidden email]<mailto:[hidden email]>> wrote:
My r-help mail is arriving out of order, so perhaps
you have already corrected this, but you need a call
to t() to make this work.  Your solution does not
give a symmetric matrix (which I think the OP wants):
 > test[lower.tri(test)] <- test[upper.tri(test)]
 > print(test)
      [,1] [,2] [,3] [,4]
 [1,]   NA    1    2    3
 [2,]    1   NA    4    5
 [3,]    2    3   NA    6
 [4,]    4    5    6   NA
but the following does
 > test[lower.tri(test)] <- t(test)[upper.tri(test)]
 > print(test)
      [,1] [,2] [,3] [,4]
 [1,]   NA    1    2    3
 [2,]    1   NA    4    5
 [3,]    2    4   NA    6
 [4,]    3    5    6   NA

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>

> -----Original Message-----
> From: [hidden email]<mailto:[hidden email]> [mailto:[hidden email]<mailto:[hidden email]>] On Behalf Of R. Michael
> Weylandt
> Sent: Monday, September 26, 2011 1:10 PM
> To: m.marcinmichal; r-help
> Subject: Re: [R] Triangular matrix upper to down
>
> On Mon, Sep 26, 2011 at 4:09 PM, R. Michael Weylandt <
> [hidden email]<mailto:[hidden email]>> wrote:
>
> > How about: test[lower.tri(test)] <- test[upper.tri(test)]
> >
> > Try it with this data so you can see that it actually works: (the ones
> > obscure possible false solutions)
> >
> >
> > test <- matrix(ncol = 4, nrow = 4)
> >  test[1, ] <-  c(NA,1,2,3)
> > test[2, ] <-  c(NA,NA,4,5)
> > test[3, ] <-  c(NA,NA,NA,6)
> >
> > test[4, ] <-  c(NA,NA,NA,NA)
> >
> > print(test)
> > test[lower.tri(test)] <- test[upper.tri(test)]
> > print(test)
> >
> > Hope this helps,
> >
> > Michael Weylandt
> >
> >
> >
> > On Mon, Sep 26, 2011 at 3:50 PM, m.marcinmichal <[hidden email]<mailto:[hidden email]>>wrote:
> >
> >> Hi,
> >> suppose that we have a triangular upper matrix A
> >>
> >> test <- matrix(ncol = 4, nrow = 4)
> >> test[1, ] <-  c(NA,1,1,1)
> >> test[2, ] <-  c(NA,NA,1,1)
> >> test[3, ] <-  c(NA,NA,NA,1)
> >> test[4, ] <-  c(NA,NA,NA,NA)
> >>
> >> I know how quickly set diagonal value diag(test) <- 1. But how quickly set
> >> down value i.e. matrix is symmetrical? Is there in r project any quickly
> >> function?
> >>
> >> Thanks,
> >>
> >> Best
> >>
> >> Marcin
> >>
> >> --
> >> View this message in context:
> >> http://r.789695.n4.nabble.com/Triangular-matrix-upper-to-down-tp3845107p3845107.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> [hidden email]<mailto:[hidden email]> 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.
> >>
> >
> >
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Triangular matrix upper to down

m.marcinmichal
In reply to this post by Michael Weylandt
Mhy,

I think that this solution It doesn't work with odd dimension. Suppose we have a reference matrix 13x13 (near i present a table format i.e save by write.table). This matrix name a response2.

"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13"
"1" 0 1.4142135623731 1 1.73205080756888 1.4142135623731 1.73205080756888 1 1.73205080756888 1.4142135623731 1 1.73205080756888 1.73205080756888 1
"2" 1.4142135623731 0 1 1 1.4142135623731 1 1.73205080756888 1 1.4142135623731 1.73205080756888 1.73205080756888 1 1.73205080756888
"3" 1 1 0 1.4142135623731 1.73205080756888 1.4142135623731 1.4142135623731 1.4142135623731 1 1.4142135623731 1.4142135623731 1.4142135623731 1.4142135623731
"4" 1.73205080756888 1 1.4142135623731 0 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"5" 1.4142135623731 1.4142135623731 1.73205080756888 1 0 1 1 1 1.4142135623731 1 1.73205080756888 1 1
"6" 1.73205080756888 1 1.4142135623731 0 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"7" 1 1.73205080756888 1.4142135623731 1.4142135623731 1 1.4142135623731 0 1.4142135623731 1 0 1.4142135623731 1.4142135623731 0
"8" 1.73205080756888 1 1.4142135623731 0 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"9" 1.4142135623731 1.4142135623731 1 1 1.4142135623731 1 1 1 0 1 1 1 1
"10" 1 1.73205080756888 1.4142135623731 1.4142135623731 1 1.4142135623731 0 1.4142135623731 1 0 1.4142135623731 1.4142135623731 0
"11" 1.73205080756888 1.73205080756888 1.4142135623731 1.4142135623731 1.73205080756888 1.4142135623731 1.4142135623731 1.4142135623731 1 1.4142135623731 0 1.4142135623731 1.4142135623731
"12" 1.73205080756888 1 1.4142135623731 0 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"13" 1 1.73205080756888 1.4142135623731 1.4142135623731 1 1.4142135623731 0 1.4142135623731 1 0 1.4142135623731 1.4142135623731 0

Suppose we have a next triangular upper matrix response1

"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13"
"1" NA 1.4142135623731 1 1.73205080756888 1.4142135623731 1.73205080756888 1 1.73205080756888 1.4142135623731 1 1.73205080756888 1.73205080756888 1
"2" NA NA 1 1 1.4142135623731 1 1.73205080756888 1 1.4142135623731 1.73205080756888 1.73205080756888 1 1.73205080756888
"3" NA NA NA 1.4142135623731 1.73205080756888 1.4142135623731 1.4142135623731 1.4142135623731 1 1.4142135623731 1.4142135623731 1.4142135623731 1.4142135623731
"4" NA NA NA NA 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"5" NA NA NA NA NA 1 1 1 1.4142135623731 1 1.73205080756888 1 1
"6" NA NA NA NA NA NA 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"7" NA NA NA NA NA NA NA 1.4142135623731 1 0 1.4142135623731 1.4142135623731 0
"8" NA NA NA NA NA NA NA NA 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"9" NA NA NA NA NA NA NA NA NA 1 1 1 1
"10" NA NA NA NA NA NA NA NA NA NA 1.4142135623731 1.4142135623731 0
"11" NA NA NA NA NA NA NA NA NA NA NA 1.4142135623731 1.4142135623731
"12" NA NA NA NA NA NA NA NA NA NA NA NA 1.4142135623731
"13" NA NA NA NA NA NA NA NA NA NA NA NA NA

Using compare operation response1 == response2 we have obtain a proper comparison i.e

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]   NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [2,]   NA   NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [3,]   NA   NA   NA TRUE TRUE TRUE TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [4,]   NA   NA   NA   NA TRUE TRUE TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [5,]   NA   NA   NA   NA   NA TRUE TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [6,]   NA   NA   NA   NA   NA   NA TRUE TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [7,]   NA   NA   NA   NA   NA   NA   NA TRUE TRUE  TRUE  TRUE  TRUE  TRUE
 [8,]   NA   NA   NA   NA   NA   NA   NA   NA TRUE  TRUE  TRUE  TRUE  TRUE
 [9,]   NA   NA   NA   NA   NA   NA   NA   NA   NA  TRUE  TRUE  TRUE  TRUE
[10,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA  TRUE  TRUE  TRUE
[11,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA  TRUE  TRUE
[12,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA  TRUE
[13,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA

But when i use this function:

response1[lower.tri(response1)] <- response1[upper.tri(response1)]

I don't receive a finding symmetric matrix. After using this function matrix response1 look like this:

"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13"
"1" NA 1.4142135623731 1 1.73205080756888 1.4142135623731 1.73205080756888 1 1.73205080756888 1.4142135623731 1 1.73205080756888 1.73205080756888 1
"2" 1.4142135623731 NA 1 1 1.4142135623731 1 1.73205080756888 1 1.4142135623731 1.73205080756888 1.73205080756888 1 1.73205080756888
"3" 1 1.4142135623731 NA 1.4142135623731 1.73205080756888 1.4142135623731 1.4142135623731 1.4142135623731 1 1.4142135623731 1.4142135623731 1.4142135623731 1.4142135623731
"4" 1 0 1.4142135623731 NA 1 0 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"5" 1.73205080756888 1 0 1 NA 1 1 1 1.4142135623731 1 1.73205080756888 1 1
"6" 1 1 1 1 0 NA 1.4142135623731 0 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"7" 1.4142135623731 1.73205080756888 0 1 1.4142135623731 1.4142135623731 NA 1.4142135623731 1 0 1.4142135623731 1.4142135623731 0
"8" 1.4142135623731 1.4142135623731 1.4142135623731 1 1 1.4142135623731 1.4142135623731 NA 1 1.4142135623731 1.4142135623731 0 1.4142135623731
"9" 1.4142135623731 1.4142135623731 1.4142135623731 1.73205080756888 1.73205080756888 1.4142135623731 0 1 NA 1 1 1 1
"10" 1.73205080756888 1 1.4142135623731 1.4142135623731 1.73205080756888 1 1 1.4142135623731 1.4142135623731 NA 1.4142135623731 1.4142135623731 0
"11" 1 1.4142135623731 1 1.4142135623731 1.4142135623731 1.4142135623731 0 1.4142135623731 1.4142135623731 0 NA 1.4142135623731 1.4142135623731
"12" 1.73205080756888 1.73205080756888 1 1 1.4142135623731 1.73205080756888 1.4142135623731 1 1 1.4142135623731 0 NA 1.4142135623731
"13" 1 1 1.4142135623731 1.4142135623731 1.73205080756888 1 0 1.73205080756888 1.4142135623731 1 1.4142135623731 1.4142135623731 NA

And it's not compare to response2 i.e response1 == response2

"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13"
"1" NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"2" TRUE NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"3" TRUE FALSE NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"4" FALSE FALSE TRUE NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"5" FALSE FALSE FALSE TRUE NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"6" FALSE TRUE FALSE FALSE FALSE NA TRUE TRUE TRUE TRUE TRUE TRUE TRUE
"7" FALSE TRUE FALSE FALSE FALSE TRUE NA TRUE TRUE TRUE TRUE TRUE TRUE
"8" FALSE FALSE TRUE FALSE TRUE FALSE TRUE NA TRUE TRUE TRUE TRUE TRUE
"9" TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE NA TRUE TRUE TRUE TRUE
"10" FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE NA TRUE TRUE TRUE
"11" FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE NA TRUE TRUE
"12" TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE NA TRUE
"13" TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE NA

Any suggestion?

Best

Marcin
Reply | Threaded
Open this post in threaded view
|

Re: Triangular matrix upper to down

m.marcinmichal
In reply to this post by William Dunlap
Ok,
thanks this solution

test[lower.tri(test)] <- t(test)[lower.tri(test)]

works perfectly well :)

Thanks

Best

marcin M.