1

Imagine we are trying to flip a square matrix and do not care if it will be flipped row- or columnwise. We have a couple of alternatives:

flip.by.col <- function(x) x[, rev(seq_len(ncol(x)))]
flip.by.row <- function(x) x[rev(seq_len(nrow(x))), ]

These matrices are not equal, but, as I said, that can be ignored.

The question is: is there any difference from a computational perspective?

One can think of a possible argument: row or column subsetting is a low-level operation that utilizes how the matrix is being stored in memory, so the answer is "presumably yes". However, I could not find any systematic difference after several tests with the following code:

require(rbenchmark)
N <- 5e3
x <- matrix(rnorm(N^2), N)
benchmark(flip.by.row(x), flip.by.col(x), replications=10)

Generally speaking, is there a situation where this can be an issue?

UPD (see discussion in comments)

To clarify, replacing one column may be faster than replacing one row of the same length: matrices are stored by columns, elements to be replaced are located sequentially. I'm not sure if it's observable though.

tonytonov
  • 25,060
  • 16
  • 82
  • 98

1 Answers1

1

In each case, flipped by row or flipping by column, the same number of points need to be moved: only elements in the middle row/column (when N is odd) stay fixed in position. So you are doing the same amount of work either way.

Note that you can get a tiny performance boost by using seq.int.

flip.by.col2 <- function(x) x[, seq.int(ncol(x), 1)]
flip.by.row2 <- function(x) x[seq.int(nrow(x), 1), ]
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Thanks. The number of elements to be moved is the same, agreed. But if a matrix is stored by columns (is it?), replacing one column generally should be faster, because elements to be replaced are stored sequentally. I know for sure that this difference is observable in C/C++ matrix operations, where switching from rows to columns can give a significant boost. Is R immune to that and why? – tonytonov Nov 21 '13 at 14:08
  • OK, good question. Yes, matrices are stored columnwise. As for the rest of it, maybe have a dig around in http://svn.r-project.org/R/trunk/src/main/subset.c to see what happens when you index matrices. My wild guess is that a lot of the time is spent allocating a new vector to copy the values into. – Richie Cotton Nov 21 '13 at 14:33