[R] vectorize a matrix conversion

Robin Hankin r.hankin at noc.soton.ac.uk
Wed Sep 12 16:32:09 CEST 2007


Hello


I have X, an n-by-n  matrix and want to convert it to Y, an
  n(n-1)/2 -by- n matrix such that each row of Y
corresponds to an element of the upper diagonal
of X.   Say row k of Y corresponds to [i,j] with i\neq j.

Then Y[i,k] = X[i,j] and Y[j,k] = X[j,i].
and Y[-c(i,j),k] = NA.

How to do this vectorizedly?

Example follows:



 > X
      [,1] [,2] [,3] [,4]
[1,]   NA   10    8    7
[2,]   10   NA    7   12
[3,]   12   13   NA    8
[4,]   13    8   12   NA
 > Y
      [,1] [,2] [,3] [,4]
[1,]   10   10   NA   NA
[2,]   12   NA    8   NA
[3,]   13   NA   NA    7
[4,]   NA   13    7   NA
[5,]   NA    8   NA   12
[6,]   NA   NA   12    8
 >

[matrix X corresponds to an all-play-all competition amongst 4  
individuals,
entry [i,j] corresponding to the  number of times individual "i" won
when competing against individual "j".  Thus individual 2 beat  
individual
3 seven times and individual 3 beat individual 2 thirteen times.
Note X[i,j] + X[j,i]=20 as there were 20 trials for each pair]


Pitiful nonvectorized code follows.

n <- nrow(X)
Y <-  matrix(NA,n*(n-1)/2,n)
k <- 1
for(i in 1:(n-1)){
   for(j in (i+1):n){
     if( !(i==j)){
       print(c(i,j,k))
       Y[k,i] <- X[i,j]
       Y[k,j] <- X[j,i]
     }
     k <- k+1
   }
}





--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



More information about the R-help mailing list