Skip to content
Prev 283981 / 398502 Next

matrix element position: from length to dim

On Thu, Feb 02, 2012 at 02:08:37PM +0100, Ana wrote:
Hi.

Assume

   d <- dim(a)
   i <- 87

Try the following two approaches.

1.

   x <- rep(FALSE, times=prod(d))
   x[i] <- TRUE
   which(array(x, dim=d), arr.ind=TRUE)

       row col
  [1,]   6  10

2.

  c((i - 1) %% d[1], (i - 1) %/% d[1]) + 1

  [1]  6 10

Hope this helps.

Petr Savicky.