Re: [R] reverse array indexing

2003-08-03 Thread Peter Wolf
Richard A. O'Keefe wrote: Jerome Asselin <[EMAIL PROTECTED]> suggests this: arr <- array(rnorm(27),c(3,3,3)) dimarr <- dim(arr) tmparr <- array(1:prod(dimarr),dimarr) sapply(c(3),function(x,tmparr) which(tmparr==x,T),tmparr=tmparr) sapply(c(3,17,13,5),functi

Re: [R] reverse array indexing

2003-07-30 Thread Richard A. O'Keefe
Jerome Asselin <[EMAIL PROTECTED]> suggests this: arr <- array(rnorm(27),c(3,3,3)) dimarr <- dim(arr) tmparr <- array(1:prod(dimarr),dimarr) sapply(c(3),function(x,tmparr) which(tmparr==x,T),tmparr=tmparr) sapply(c(3,17,13,5),function(x,tmparr) which(tmparr==

Re: [R] reverse array indexing

2003-07-30 Thread Peter Dalgaard BSA
"Buchsbaum, Brad (NIH/NIMH)" <[EMAIL PROTECTED]> writes: > Hi, > > Suppose I have a multidimensional array: > > tmp <- array(1:8, c(2,2,2)) > > is there a function out there that, given a one-dimensional array index, > will > return the separate indices for each array dimension? > > for instan

Re: [R] reverse array indexing

2003-07-30 Thread Jerome Asselin
Try this... Perhaps this will help you do what you want. The key idea is to use which() with option arr.ind = TRUE. arr <- array(rnorm(27),c(3,3,3)) dimarr <- dim(arr) tmparr <- array(1:prod(dimarr),dimarr) sapply(c(3),function(x,tmparr) which(tmparr==x,T),tmparr=tmparr) sapply(c(3,17,13,5),func

Re: [R] reverse array indexing

2003-07-30 Thread Patrick Burns
I believe that the function below satisfies the request: rev.index <- function (x, dims) { ld <- length(dims) cumdim <- cumprod(dims) ans <- array(NA, c(length(x), ld)) ans[, ld] <- (x-1) %/% cumdim[ld - 1] + 1 ans[, ld-1] <- (x-1) %% cumdim[ld - 1] + 1 if

[R] reverse array indexing

2003-07-30 Thread Buchsbaum, Brad (NIH/NIMH)
Hi, Suppose I have a multidimensional array: tmp <- array(1:8, c(2,2,2)) is there a function out there that, given a one-dimensional array index, will return the separate indices for each array dimension? for instance, tmp[8] is equivalent to tmp[2,2,2]. I'd like to derive the vector (2,2,2)