Fast matrix subsetting via '[': by rows, by columns or doesn't matter?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 23:15:06

问题


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


来源:https://stackoverflow.com/questions/20121471/fast-matrix-subsetting-via-by-rows-by-columns-or-doesnt-matter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!