Continuing with Daniel's example, but with different data values


a <- sample(24)
a
dim(a) <- c(3,4,2)
a
as.vector(a)

## for an array with
## dim(a) == c(3,4,2)
## a[i,j,k] means select the element in position
##    i + (j-1)*3 + (k-1)*3*4

index <- function(i,j,k) {
   i + (j-1)*3 + (k-1)*3*4
}

## find the vector position described by row 2, column 1, layer 2
index(2,1,2)    ## this is the position in the original vector
a[2,1,2]        ## this is the value in that position with 3D indexing
a[index(2,1,2)] ## this is the same value with 1D vector indexing
a[14]           ## this is the same value with 1D vector indexing

## find the position in row 3, column 4, layer 1
index(3,4,1)    ## this is the position in the original vector
a[3,4,1]        ## this is the value in that position with 3D indexing
a[index(3,4,1)] ## this is the same value with 1D vector indexing
a[12]           ## this is the same value with 1D vector indexing


index(1,1,1)    ## this is the position in the original vector
index(2,1,1)    ## this is the position in the original vector
index(3,1,1)    ## this is the position in the original vector
index(1,2,1)    ## this is the position in the original vector
index(2,2,1)    ## this is the position in the original vector
index(3,2,1)    ## this is the position in the original vector
index(1,3,1)    ## this is the position in the original vector
index(2,3,1)    ## this is the position in the original vector
index(3,3,1)    ## this is the position in the original vector
index(1,4,1)    ## this is the position in the original vector
index(2,4,1)    ## this is the position in the original vector
index(3,4,1)    ## this is the position in the original vector
index(1,1,2)    ## this is the position in the original vector
index(2,1,2)    ## this is the position in the original vector
index(3,1,2)    ## this is the position in the original vector
index(1,2,2)    ## this is the position in the original vector
index(2,2,2)    ## this is the position in the original vector
index(3,2,2)    ## this is the position in the original vector
index(1,3,2)    ## this is the position in the original vector
index(2,3,2)    ## this is the position in the original vector
index(3,3,2)    ## this is the position in the original vector
index(1,4,2)    ## this is the position in the original vector
index(2,4,2)    ## this is the position in the original vector
index(3,4,2)    ## this is the position in the original vector

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to