mapToGrid: Map reference array positions to grid positions and...

Description Usage Arguments Value See Also Examples

Description

Use mapToGrid() to map a set of reference array positions to grid positions. Use mapToRef() for the reverse mapping.

Usage

1
2
3
mapToGrid(aind, grid, linear=FALSE)

mapToRef(major, minor, grid, linear=FALSE)

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 reference array of grid.

For convenience, aind can also be specified as a vector with one element per dimension in grid, 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 > refdim(grid)[j]. What those values will be mapped to is undefined.

grid

An ArrayGrid object.

linear

TRUE or FALSE. Controls the format of the output for mapToGrid and the input for mapToRef.

By default (i.e. when linear is FALSE), the major and minor indices returned by mapToGrid (or taken by mapToRef) are array indices (i.e. n-uplets). When linear is set to TRUE, they are returned (or taken) as linear indices.

major, minor

The major and minor components as returned by mapToGrid.

Value

See Also

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## Create an arbitrary-spaced grid on top of a 15 x 9 matrix:
grid2 <- ArbitraryArrayGrid(list(c(2L, 7:10, 13L, 15L), c(5:6, 6L, 9L)))

## Create a set of reference array positions:
aind <- rbind(c( 2, 5),  # bottom right corner of 1st grid element
              c( 3, 1),  # top left corner of 2nd grid element
              c(14, 9),  # top right corner of last grid element
              c(15, 7),  # bottom left corner of last grid element
              c(15, 9))  # bottom right corner of last grid element

## Map them to grid positions:
majmin <- mapToGrid(aind, grid2)
majmin

## Reverse mapping:
aind2 <- mapToRef(majmin$major, majmin$minor, grid2)
stopifnot(all.equal(aind2, aind))

majmin <- mapToGrid(aind, grid2, linear=TRUE)
majmin
aind2 <- mapToRef(majmin$major, majmin$minor, grid2, linear=TRUE)
stopifnot(all.equal(aind2, aind))

## Map all the valid positions:
all_positions <- seq_len(prod(refdim(grid2)))
aind <- arrayInd(all_positions, refdim(grid2))
majmin <- data.frame(mapToGrid(aind, grid2, linear=TRUE))
majmin

## Sanity checks:
min_by_maj <- split(majmin$minor,
                    factor(majmin$major, levels=seq_along(grid2)))
stopifnot(identical(lengths(min_by_maj, use.names=FALSE), lengths(grid2)))
stopifnot(all(mapply(isSequence, min_by_maj, lengths(min_by_maj))))
aind2 <- mapToRef(majmin$major, majmin$minor, grid2, linear=TRUE)
stopifnot(identical(aind2, aind))

## More mapping:
grid4 <- RegularArrayGrid(c(50, 20), spacings=c(15L, 9L))
aind <- rbind(c( 1,  1),
              c( 2,  1),
              c( 3,  1),
              c(16,  1),
              c(16,  2),
              c(16, 10),
              c(27, 18))

mapToGrid(aind, grid4)
mapToGrid(aind, grid4, linear=TRUE)

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