linearInd: Converting array indices into linear indices

Description Usage Arguments Value See Also Examples

View source: R/linearInd.R

Description

linearInd performs the reverse conversion of base::arrayInd, that is, it converts so-called array indices (i.e. n-uplets) into linear indices.

Usage

1

Arguments

aind

Typically a numeric matrix like one returned by base::arrayInd, that is, a matrix where each row is an n-uplet representing an array index. Each array index must describe a position relative to the implicit array i.e. to the array whose dimensions are specified via the dim argument.

For convenience, aind can also be specified as a vector with one element per dimension in the implicit array, in which case it will be treated like a 1-row matrix.

Note that no bounds checking is performed, that is, values in the j-th column of aind can be < 1 or > dim[j].

dim

An integer vector containing the dimensions of the underlying array.

Note that dim can also be an integer matrix, in which case it must have the same shape as aind, that is, 1 row per row in aind and 1 column per dimension.

Value

An integer vector with one element per row in aind if aind is a matrix.

A single integer if aind is a vector.

See Also

arrayInd in the base package for the reverse conversion.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
dim <- 4:2
linearInd(c(4, 3, 1), dim)
linearInd(c(4, 3, 2), dim)

aind <- rbind(c(1, 1, 1),
              c(2, 1, 1),
              c(3, 1, 1),
              c(4, 1, 1),
              c(1, 2, 1),
              c(1, 1, 2),
              c(4, 3, 2))

linearInd(aind, dim)

## With a matrix of dimensions:

dims <- rbind(c(4L, 3L),
              c(5L, 3L),
              c(6L, 3L))

aind <- rbind(c(1,  2),
              c(1,  2),
              c(1,  2))

linearInd(aind, dims)

## Sanity checks:

dim <- c(33:30, 45L, 30L)
stopifnot(linearInd(rep(1, 6), dim) == 1)
stopifnot(linearInd(dim, dim) == prod(dim))

stopifnot(identical(linearInd(arrayInd(1:120, 6:4), 6:4), 1:120))
stopifnot(identical(linearInd(arrayInd(840:1, 4:7), 4:7), 840:1))

DelayedArray documentation built on Nov. 1, 2018, 2:27 a.m.