It is possible to use a matrix of integers as an index. In this case, the number of columns of the matrix should match the number of dimensions of the structure, and the result will be a vector with length as the number of rows of the matrix. The following example shows how to extract the elements m[1, 1] and m[2, 2]in one operation.

> m <- matrix(1:4, 2)

> m [,1] [,2]

[1,] 1 3

[2,] 2 4

> i <- matrix(c(1, 1, 2, 2), 2, byrow = TRUE)

> i [,1] [,2]

[1,] 1 1

[2,] 2 2

> m[i]

[1] 1 4

Negative indices are not allowed in indexing matrices. NA and zero values are allowed: rows in an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.

consult:http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Indexing-matrices-and-arrays

In general, above is the case where i is two dimensional and specifies the row and column number for m. but, if i is three dimensional, then m[i] list the elements of m by a numbering by row, then by column:

1 2 3

4 5 6

7 8 9

if

>m <- matrix(10:18, 3)

>i <- matrix(c(1:9), 3, byrow=TRUE)

>m[i]

[1] 10 11 12 13 14 15 16 17 18 NA

then switch:

>i <- matrix(c(1:9), 3)

> m[i]

[1] 10 11 12 13 14 15 16 17 18

log file: see here

> m <- matrix(1:4, 2)

> m [,1] [,2]

[1,] 1 3

[2,] 2 4

> i <- matrix(c(1, 1, 2, 2), 2, byrow = TRUE)

> i [,1] [,2]

[1,] 1 1

[2,] 2 2

> m[i]

[1] 1 4

Negative indices are not allowed in indexing matrices. NA and zero values are allowed: rows in an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.

consult:http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Indexing-matrices-and-arrays

In general, above is the case where i is two dimensional and specifies the row and column number for m. but, if i is three dimensional, then m[i] list the elements of m by a numbering by row, then by column:

1 2 3

4 5 6

7 8 9

if

>m <- matrix(10:18, 3)

>i <- matrix(c(1:9), 3, byrow=TRUE)

>m[i]

[1] 10 11 12 13 14 15 16 17 18 NA

then switch:

>i <- matrix(c(1:9), 3)

> m[i]

[1] 10 11 12 13 14 15 16 17 18

log file: see here