[Q] extracting lower diagonal elements of a matrix

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

[Q] extracting lower diagonal elements of a matrix

Taka Matzmoto
Hi R users

I like to extract lower diagonal elements of a matrix in such a way like,
data[1,2], data[1,3],
...., data[5,6] are extracted from a matrix called 'data'

This short script below is what I have written so far.

##########################################
data <- matrix(rnorm(36,0,1),nrow=6)
temp<-c()
for (i in 1:(nrow(data)-1))
{
    for (j in (i+1):nrow(data))
    {
        temp<-append(temp,data[j,i])
    }
}
##########################################

Is there any function for this?  or is there any elegant way to do this
task?

Thanks in advance.

TM

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

Re: [Q] extracting lower diagonal elements of a matrix

Jacques VESLOT
try:
as.vector(as.dist(data))


Taka Matzmoto a écrit :

>Hi R users
>
>I like to extract lower diagonal elements of a matrix in such a way like,
>data[1,2], data[1,3],
>...., data[5,6] are extracted from a matrix called 'data'
>
>This short script below is what I have written so far.
>
>##########################################
>data <- matrix(rnorm(36,0,1),nrow=6)
>temp<-c()
>for (i in 1:(nrow(data)-1))
>{
>    for (j in (i+1):nrow(data))
>    {
>        temp<-append(temp,data[j,i])
>    }
>}
>##########################################
>
>Is there any function for this?  or is there any elegant way to do this
>task?
>
>Thanks in advance.
>
>TM
>
>______________________________________________
>[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
>
>  
>

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

Re: [Q] extracting lower diagonal elements of a matrix

Christos Hatzis
In reply to this post by Taka Matzmoto
Try

s <- matrix(rnorm(36,0,1),nrow=6)
s[col(s)<row(s)]

Courtesy of V&R.

-Christos

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Taka Matzmoto
Sent: Friday, January 27, 2006 1:13 AM
To: [hidden email]
Subject: [R] [Q] extracting lower diagonal elements of a matrix

Hi R users

I like to extract lower diagonal elements of a matrix in such a way like,
data[1,2], data[1,3], ...., data[5,6] are extracted from a matrix called
'data'

This short script below is what I have written so far.

##########################################
data <- matrix(rnorm(36,0,1),nrow=6)
temp<-c()
for (i in 1:(nrow(data)-1))
{
    for (j in (i+1):nrow(data))
    {
        temp<-append(temp,data[j,i])
    }
}
##########################################

Is there any function for this?  or is there any elegant way to do this
task?

Thanks in advance.

TM

______________________________________________
[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

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

Re: [Q] extracting lower diagonal elements of a matrix

vincent-18
In reply to this post by Taka Matzmoto
?lower.tri
hih

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

Re: [Q] extracting lower diagonal elements of a matrix

Philippe Grosjean
In reply to this post by Christos Hatzis
Try:
 > data[lower.tri(data)]

(the same as data[col(data) < row(data)], but using the dedicated function).
Best,

Philippe Grosjean

Christos Hatzis wrote:

> Try
>
> s <- matrix(rnorm(36,0,1),nrow=6)
> s[col(s)<row(s)]
>
> Courtesy of V&R.
>
> -Christos
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Taka Matzmoto
> Sent: Friday, January 27, 2006 1:13 AM
> To: [hidden email]
> Subject: [R] [Q] extracting lower diagonal elements of a matrix
>
> Hi R users
>
> I like to extract lower diagonal elements of a matrix in such a way like,
> data[1,2], data[1,3], ...., data[5,6] are extracted from a matrix called
> 'data'
>
> This short script below is what I have written so far.
>
> ##########################################
> data <- matrix(rnorm(36,0,1),nrow=6)
> temp<-c()
> for (i in 1:(nrow(data)-1))
> {
>     for (j in (i+1):nrow(data))
>     {
>         temp<-append(temp,data[j,i])
>     }
> }
> ##########################################
>
> Is there any function for this?  or is there any elegant way to do this
> task?
>
> Thanks in advance.
>
> TM
>
> ______________________________________________
> [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
>
> ______________________________________________
> [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
>
>

______________________________________________
[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