Skip to content
Prev 68537 / 398506 Next

Index matrix to pick elements from 3-dimensional matrix

Hello Juhana

try this (but there must be a better way!)



stratified.select <- function(A,J){
   out <- sapply(J,function(i){sample(A[,,i],1)})
   attributes(out) <- attributes(J)
   return(out)
}

A <- array(letters[1:12],c(2,2,3))
J <- matrix(c(1,2,3,3),2,2)


R>  stratified.select(A,J)
      [,1] [,2]
[1,] "b"  "i"
[2,] "g"  "k"
R>   stratified.select(A,J)
      [,1] [,2]
[1,] "d"  "j"
[2,] "f"  "l"
R>


best wishes

Robin
On Apr 26, 2005, at 05:16 am, juhana vartiainen wrote:

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