Description Usage Arguments Value Examples
Get Values in a Matrix by Pair-wise Indices
1 | pindex(m, i, j)
|
m |
A matrix or a 3-dimension array. |
i |
Row indices or the indices in the first dimension. |
j |
Column indicies or the indices in the second dimension. |
If m
is a matrix, the value returned is a vector c(m[i1, j1], m[i2, j2], ...)
'.
If m
is an array, the value returned is a matrix rbind(m[i1, j1, ], m[i2, j2, ], ...)
'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | m = matrix(rnorm(100), 10)
m2 = m[m > 0]
ind = do.call("rbind", lapply(1:10, function(ci) {
i = which(m[, ci] > 0)
cbind(i = i, j = rep(ci, length(i)))
}))
pindex(m, ind[, 1], ind[, 2])
identical(pindex(m, ind[, 1], ind[, 2]), m[m > 0])
# 3d array
arr = array(1:27, dim = c(3, 3, 3))
pindex(arr, 1:2, 2:3)
identical(pindex(arr, 1:2, 2:3),
rbind(arr[1, 2, ], arr[2, 3, ]))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.