Flip an array along some dimension(s), e.g. flip a matrix upside down or left to right.

flip(x, dim = 1)

Arguments

x

an array

dim

dimension(s) along which to flip (in sequence), defaults to 1 (i.e. flip rows).

Value

The array x having the dim dimension flipped.

Examples

# flip a matrix x<-matrix(1:6,2) x
#> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 2 4 6
# flip upside down flip(x,1)
#> [,1] [,2] [,3] #> [1,] 2 4 6 #> [2,] 1 3 5
# flip left to right flip(x,2)
#> [,1] [,2] [,3] #> [1,] 5 3 1 #> [2,] 6 4 2
# flip both upside down and left to right flip(x,1:2)
#> [,1] [,2] [,3] #> [1,] 6 4 2 #> [2,] 5 3 1
# flip a vector v<-1:10 v
#> [1] 1 2 3 4 5 6 7 8 9 10
flip(v)
#> [1] 10 9 8 7 6 5 4 3 2 1
# flip an array a<-array(1:prod(2:4),2:4) a
#> , , 1 #> #> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 2 4 6 #> #> , , 2 #> #> [,1] [,2] [,3] #> [1,] 7 9 11 #> [2,] 8 10 12 #> #> , , 3 #> #> [,1] [,2] [,3] #> [1,] 13 15 17 #> [2,] 14 16 18 #> #> , , 4 #> #> [,1] [,2] [,3] #> [1,] 19 21 23 #> [2,] 20 22 24 #>
# flip along dim 1 flip(a,1)
#> , , 1 #> #> [,1] [,2] [,3] #> [1,] 2 4 6 #> [2,] 1 3 5 #> #> , , 2 #> #> [,1] [,2] [,3] #> [1,] 8 10 12 #> [2,] 7 9 11 #> #> , , 3 #> #> [,1] [,2] [,3] #> [1,] 14 16 18 #> [2,] 13 15 17 #> #> , , 4 #> #> [,1] [,2] [,3] #> [1,] 20 22 24 #> [2,] 19 21 23 #>
# flip along dim 2 flip(a,2)
#> , , 1 #> #> [,1] [,2] [,3] #> [1,] 5 3 1 #> [2,] 6 4 2 #> #> , , 2 #> #> [,1] [,2] [,3] #> [1,] 11 9 7 #> [2,] 12 10 8 #> #> , , 3 #> #> [,1] [,2] [,3] #> [1,] 17 15 13 #> [2,] 18 16 14 #> #> , , 4 #> #> [,1] [,2] [,3] #> [1,] 23 21 19 #> [2,] 24 22 20 #>
# flip along dim 3 flip(a,3)
#> , , 1 #> #> [,1] [,2] [,3] #> [1,] 19 21 23 #> [2,] 20 22 24 #> #> , , 2 #> #> [,1] [,2] [,3] #> [1,] 13 15 17 #> [2,] 14 16 18 #> #> , , 3 #> #> [,1] [,2] [,3] #> [1,] 7 9 11 #> [2,] 8 10 12 #> #> , , 4 #> #> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 2 4 6 #>