Make vector positions from a (non-symmetric) array index respecting dim and dimorder

arrayIndex2vectorIndex(x, dim = NULL, dimorder = NULL, vw = NULL)

Arguments

x

an n by m matrix with n m-dimensional array indices

dim

NULL or dim

dimorder

NULL or dimorder

vw

NULL or integer vector[3] or integer matrix[3,m], see details

Details

The fastest rotating dimension is dim[dimorder[1]], then dim[dimorder[2]], and so forth.
The parameters 'x' and 'dim' may refer to a subarray of a larger array, in this case, the array indices 'x' are interpreted as 'vw[1,] + x' within the larger array 'as.integer(colSums(vw))'.

Value

a vector of indices in seq_len(prod(dim)) (or seq_len(prod(colSums(vw))))

Author

Jens Oehlschlägel

Examples

  x <- matrix(1:12, 3, 4)
  x
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12
  arrayIndex2vectorIndex(cbind(as.vector(row(x)), as.vector(col(x)))
  , dim=dim(x))
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12
  arrayIndex2vectorIndex(cbind(as.vector(row(x)), as.vector(col(x)))
  , dim=dim(x), dimorder=2:1)
#>  [1]  1  5  9  2  6 10  3  7 11  4  8 12
  matrix(1:30, 5, 6)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    6   11   16   21   26
#> [2,]    2    7   12   17   22   27
#> [3,]    3    8   13   18   23   28
#> [4,]    4    9   14   19   24   29
#> [5,]    5   10   15   20   25   30
  arrayIndex2vectorIndex(cbind(as.vector(row(x)), as.vector(col(x)))
  , vw=rbind(c(0,1), c(3,4), c(2,1)))
#>  [1]  6  7  8 11 12 13 16 17 18 21 22 23
  arrayIndex2vectorIndex(cbind(as.vector(row(x)), as.vector(col(x)))
  , vw=rbind(c(0,1), c(3,4), c(2,1)), dimorder=2:1)
#>  [1]  2  8 14  3  9 15  4 10 16  5 11 17