Fast matrix subsetting via '[': by rows, b

2019-08-15 05:33发布

问题:

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.

回答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), ]


标签: r matrix subset