1

In Matlab, I know there are some functions that can flip matrix, say flip left and right (horizontal) or up and down (vertical), but I have no idea if there are similar ones in R.

For example, assuming we have a matrix m <- matrix(1:12,nrow = 3), we want

  • Horizontal flip to get mh
     [,1] [,2] [,3] [,4]
[1,]   10    7    4    1
[2,]   11    8    5    2
[3,]   12    9    6    3
  • Vertical flip to get mv
     [,1] [,2] [,3] [,4]
[1,]    3    6    9   12
[2,]    2    5    8   11
[3,]    1    4    7   10

2 Answers2

3

I think pracma package could help you

mh <- pracma::fliplr(m)

and

mv <- pracma::flipud(m)

such that

> mh
     [,1] [,2] [,3] [,4]
[1,]   10    7    4    1
[2,]   11    8    5    2
[3,]   12    9    6    3

> mv
     [,1] [,2] [,3] [,4]
[1,]    3    6    9   12
[2,]    2    5    8   11
[3,]    1    4    7   10
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
3

We can do the sequence reversed as index for column, row to get the flipped datasets

m[, ncol(m):1]
#     [,1] [,2] [,3] [,4]
#[1,]   10    7    4    1
#[2,]   11    8    5    2
#[3,]   12    9    6    3

m[nrow(m):1, ]
#     [,1] [,2] [,3] [,4]
#[1,]    3    6    9   12
#[2,]    2    5    8   11
#[3,]    1    4    7   10
akrun
  • 874,273
  • 37
  • 540
  • 662