reordertensor.RdThis permutes tensor dimensions like aperm. However the interface is more flexible since not all dimensions have to given and names can be used instead of numbers.
# S3 method for class 'tensor'
reorder(x,i=NULL,...,by=NULL)
<!-- %reorder.tidx(x,i,...) -->reorder.tensor returns a tensor equal to x but stored with a different
sequence of dimensions.
the remaining dimensions keep their relative sequence and follow at the end of the dimension attribute.
A <- to.tensor(1:20,c(A=2,B=2,C=5))
A
#> , , 1
#>
#> B
#> A [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> , , 2
#>
#> B
#> A [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> , , 3
#>
#> B
#> A [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
#> , , 4
#>
#> B
#> A [,1] [,2]
#> [1,] 13 15
#> [2,] 14 16
#>
#> , , 5
#>
#> B
#> A [,1] [,2]
#> [1,] 17 19
#> [2,] 18 20
#>
#> attr(,"class")
#> [1] "tensor"
reorder(A,"C")
#> , , 1
#>
#> A
#> C [,1] [,2]
#> [1,] 1 2
#> [2,] 5 6
#> [3,] 9 10
#> [4,] 13 14
#> [5,] 17 18
#>
#> , , 2
#>
#> A
#> C [,1] [,2]
#> [1,] 3 4
#> [2,] 7 8
#> [3,] 11 12
#> [4,] 15 16
#> [5,] 19 20
#>
#> attr(,"class")
#> [1] "tensor"
reorder(A,"B")
#> , , 1
#>
#> A
#> B [,1] [,2]
#> [1,] 1 2
#> [2,] 3 4
#>
#> , , 2
#>
#> A
#> B [,1] [,2]
#> [1,] 5 6
#> [2,] 7 8
#>
#> , , 3
#>
#> A
#> B [,1] [,2]
#> [1,] 9 10
#> [2,] 11 12
#>
#> , , 4
#>
#> A
#> B [,1] [,2]
#> [1,] 13 14
#> [2,] 15 16
#>
#> , , 5
#>
#> A
#> B [,1] [,2]
#> [1,] 17 18
#> [2,] 19 20
#>
#> attr(,"class")
#> [1] "tensor"