rray_split: Split an array along axes

Description Usage Arguments Details Value Examples

View source: R/split.R

Description

rray_split() splits x into equal pieces, splitting along the axes.

Usage

1
rray_split(x, axes = NULL)

Arguments

x

A vector, matrix, array, or rray.

axes

An integer vector. The axes to split on. The default splits along all axes.

Details

rray_split() works by splitting along the axes. The result is a list of sub arrays, where the axes of each sub array have been reduced to length 1. This is consistent with how reducers like rray_sum() work. As an example, splitting a (2, 3, 5) array along axes = c(2, 3) would result in a list of 15 (from 3 * 5) sub arrays, each with shape (2, 1, 1).

Value

A list of sub arrays of type x.

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
x <- matrix(1:8, ncol = 2)

# Split the rows
# (4, 2) -> (1, 2)
rray_split(x, 1)

# Split the columns
# (4, 2) -> (4, 1)
rray_split(x, 2)

# Split along multiple dimensions
# (4, 2) -> (1, 1)
rray_split(x, c(1, 2))

# The above split is the default behavior
rray_split(x)

# You can technically split with a size 0 `axes`
# argument, which essentially requests no axes
# to be split and is the same as `list(x)`
rray_split(x, axes = integer(0))

# ---------------------------------------------------------------------------
# 4 dimensional example

x_4d <- rray(
  x = 1:16,
  dim = c(2, 2, 2, 2),
  dim_names = list(
    c("r1", "r2"),
    c("c1", "c2"),
    c("d1", "d2"),
    c("e1", "e2")
  )
)

# Split along the 1st dimension (rows)
# (2, 2, 2, 2) -> (1, 2, 2, 2)
rray_split(x_4d, 1)

# Split along columns
# (2, 2, 2, 2) -> (2, 1, 2, 2)
rray_split(x_4d, 2)

# Probably the most useful thing you might do
# is use this to split the 4D array into a set
# of 4 2D matrices.
rray_split(x_4d, c(3, 4))

rray documentation built on July 23, 2019, 5:04 p.m.