Taka Matzmoto
2006-Jan-27 06:12 UTC
[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
Jacques VESLOT
2006-Jan-27 06:20 UTC
[R] [Q] extracting lower diagonal elements of a matrix
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 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >
Christos Hatzis
2006-Jan-27 06:22 UTC
[R] [Q] extracting lower diagonal elements of a matrix
Try
s <- matrix(rnorm(36,0,1),nrow=6)
s[col(s)<row(s)]
Courtesy of V&R.
-Christos
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Taka Matzmoto
Sent: Friday, January 27, 2006 1:13 AM
To: r-help at stat.math.ethz.ch
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
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html