rray_split()
splits x
into equal pieces, splitting along the axes
.
rray_split(x, axes = NULL)
x | A vector, matrix, array, or rray. |
---|---|
axes | An integer vector. The axes to split on. The default splits along all axes. |
A list of sub arrays of type x
.
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)
.
#> [[1]] #> [,1] [,2] #> [1,] 1 5 #> #> [[2]] #> [,1] [,2] #> [1,] 2 6 #> #> [[3]] #> [,1] [,2] #> [1,] 3 7 #> #> [[4]] #> [,1] [,2] #> [1,] 4 8 #># Split the columns # (4, 2) -> (4, 1) rray_split(x, 2)#> [[1]] #> [,1] #> [1,] 1 #> [2,] 2 #> [3,] 3 #> [4,] 4 #> #> [[2]] #> [,1] #> [1,] 5 #> [2,] 6 #> [3,] 7 #> [4,] 8 #>#> [[1]] #> [,1] #> [1,] 1 #> #> [[2]] #> [,1] #> [1,] 2 #> #> [[3]] #> [,1] #> [1,] 3 #> #> [[4]] #> [,1] #> [1,] 4 #> #> [[5]] #> [,1] #> [1,] 5 #> #> [[6]] #> [,1] #> [1,] 6 #> #> [[7]] #> [,1] #> [1,] 7 #> #> [[8]] #> [,1] #> [1,] 8 #># The above split is the default behavior rray_split(x)#> [[1]] #> [,1] #> [1,] 1 #> #> [[2]] #> [,1] #> [1,] 2 #> #> [[3]] #> [,1] #> [1,] 3 #> #> [[4]] #> [,1] #> [1,] 4 #> #> [[5]] #> [,1] #> [1,] 5 #> #> [[6]] #> [,1] #> [1,] 6 #> #> [[7]] #> [,1] #> [1,] 7 #> #> [[8]] #> [,1] #> [1,] 8 #># 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))#> [[1]] #> [,1] [,2] #> [1,] 1 5 #> [2,] 2 6 #> [3,] 3 7 #> [4,] 4 8 #># --------------------------------------------------------------------------- # 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)#> [[1]] #> <rray<int>[,2,2,2][1]> #> , , d1, e1 #> #> c1 c2 #> r1 1 3 #> #> , , d2, e1 #> #> c1 c2 #> r1 5 7 #> #> , , d1, e2 #> #> c1 c2 #> r1 9 11 #> #> , , d2, e2 #> #> c1 c2 #> r1 13 15 #> #> #> [[2]] #> <rray<int>[,2,2,2][1]> #> , , d1, e1 #> #> c1 c2 #> r2 2 4 #> #> , , d2, e1 #> #> c1 c2 #> r2 6 8 #> #> , , d1, e2 #> #> c1 c2 #> r2 10 12 #> #> , , d2, e2 #> #> c1 c2 #> r2 14 16 #> #># Split along columns # (2, 2, 2, 2) -> (2, 1, 2, 2) rray_split(x_4d, 2)#> [[1]] #> <rray<int>[,1,2,2][2]> #> , , d1, e1 #> #> c1 #> r1 1 #> r2 2 #> #> , , d2, e1 #> #> c1 #> r1 5 #> r2 6 #> #> , , d1, e2 #> #> c1 #> r1 9 #> r2 10 #> #> , , d2, e2 #> #> c1 #> r1 13 #> r2 14 #> #> #> [[2]] #> <rray<int>[,1,2,2][2]> #> , , d1, e1 #> #> c2 #> r1 3 #> r2 4 #> #> , , d2, e1 #> #> c2 #> r1 7 #> r2 8 #> #> , , d1, e2 #> #> c2 #> r1 11 #> r2 12 #> #> , , d2, e2 #> #> c2 #> r1 15 #> r2 16 #> #># 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))#> [[1]] #> <rray<int>[,2,1,1][2]> #> , , d1, e1 #> #> c1 c2 #> r1 1 3 #> r2 2 4 #> #> #> [[2]] #> <rray<int>[,2,1,1][2]> #> , , d2, e1 #> #> c1 c2 #> r1 5 7 #> r2 6 8 #> #> #> [[3]] #> <rray<int>[,2,1,1][2]> #> , , d1, e2 #> #> c1 c2 #> r1 9 11 #> r2 10 12 #> #> #> [[4]] #> <rray<int>[,2,1,1][2]> #> , , d2, e2 #> #> c1 c2 #> r1 13 15 #> r2 14 16 #> #>