Converting array and vector Indices Calculate the vector index from array indices, and vice versa.

Share:

Description

arrays are numerics with a dim attribute and are stored with the first index moving fastest (i.e. by column). They can be indexed both ways.

Usage

1
2
3
array2vec(iarr, dim)

vec2array(ivec, dim)

Arguments

iarr

vector with the indices into the array dimensions

dim

vector with the array dimensions, as returned by dim (x)

ivec

scalar with the index into the vector

Value

array2vec returns a scalar, vec2array a matrix.

Author(s)

C. Beleites

See Also

see Extract on the difference of indexing an array with a vector or a matrix.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
arr <- array (rnorm (24), dim = 2 : 4)
arr

v <- matrix(c(2, 2, 2), nrow = 1)
i <- array2vec (v, dim = dim (arr))
i
arr[v]
arr[i]

arr[c(2, 2, 2)] ## indexing with a vector
arr[2]

 
i <- 14
v <- vec2array (i, dim = dim (arr))
v
arr [v]
arr [i]