通过“["快速矩阵子集:按行、按列还是无关紧要? [英] Fast matrix subsetting via '[': by rows, by columns or doesn't matter?
问题描述
想象一下,我们正在尝试翻转一个方阵并且不关心它是按行还是按列翻转.我们有几个选择:
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(见评论中的讨论)
澄清一下,替换一列可能比替换相同长度的一行更快:矩阵按列存储,要替换的元素按顺序定位.我不确定它是否可以观察到.
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.
推荐答案
无论是按行翻转还是按列翻转,都需要移动相同数量的点:仅中间行/列的元素(当N
是奇数)保持固定位置.因此,无论哪种方式,您都在做相同数量的工作.
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.
请注意,您可以通过使用 seq.int
获得微小的性能提升.
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), ]
这篇关于通过“["快速矩阵子集:按行、按列还是无关紧要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!