简体   繁体   English

带有数字向量(不是矩阵向量)的 MVM 以及如何避免双重转置?

[英]MVM with a numeric vector (not a matrix vector) and how to avoid double transposition?

I have the following simplified case which is actually a much bigger performance hotspot:我有以下简化案例,它实际上是一个更大的性能热点:

epsilon <- c(-3, -4)
str(epsilon)
num [1:2] -3 -4

Q <- matrix(c(2, 1, 0, 3, 1, 0), nrow=3, ncol=2)
str(Q)
num [1:3, 1:2] 2 1 0 3 1 0
Q
     [,1] [,2]
[1,]    2    3
[2,]    1    1
[3,]    0    0

This is the correct result I need but it requires double-transposition ie expensive for large Qs:这是我需要的正确结果,但它需要双转位,即对于大 Q 来说很昂贵:

t(epsilon*t(Q))
     [,1] [,2]
[1,]   -6  -12
[2,]   -3   -4
[3,]    0    0

However if I simply do the following I get different results (ie the second row is flipped):但是,如果我只是执行以下操作,则会得到不同的结果(即第二行被翻转):

Q*epsilon
     [,1] [,2]
[1,]   -6  -12
[2,]   -4   -3
[3,]    0    0

Mathematically speaking (epsilon*Q^T)^T should be the same as Q*epsilon^T but that's not what I want (doing Q%*%epsilon it would generate a 3x1 result).从数学上讲(epsilon*Q^T)^T应该与Q*epsilon^T相同,但这不是我想要的(做Q%*%epsilon它会产生 3x1 结果)。

Is there a way to achieve this without double transposing the potentially very large Q?有没有办法在不双重转置可能非常大的 Q 的情况下实现这一目标?

It would be easier to rep licate with either col providing the index of the columns of 'Q' so that both elements are of the same length这将是容易rep licate与任一col提供的“Q”的列的索引,使得两个元件是相同的长度的

Q * epsilon[col(Q)]

Or use rep或者使用rep

Q * rep(epsilon, each = nrow(Q))

Or with sweep或者用sweep

sweep(Q, 2, epsilon, `*`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用列值的数值向量对数值矩阵进行子集 - Subset a numeric matrix with a numeric vector of column values 如何根据逻辑从数值矩阵生成向量或因子(no for loop) - how to generate a vector or factor from a numeric matrix based on logic (no for loop) 如何网格化数值向量和字符矩阵? - How Do I Mesh a Numeric Vector and a Character Matrix? 如何从TDM矩阵中分离出二元数的数值向量 - How to split out numeric vector of bigrams from TDM matrix 为什么此矩阵不是数字? 然后`as.numeric`破坏矩阵并返回向量 - Why is this matrix not numeric? Then `as.numeric` destroys the matrix and return a vector 如何从数值向量自动生成颜色向量 - How to autogenerate color vector from a numeric vector neura.net:需要数字/复杂矩阵/向量 arguments - neuralnet: requires numeric/complex matrix/vector arguments 为什么as.numeric将矩阵转换为向量,如何将结果保留为矩阵? - Why does as.numeric convert a matrix to a vector, and how do I keep the result as a matrix? 如何将矩阵从矩阵列表转换为未列出的二维数字/矩阵而不展平为 R 中的向量? - How to convert a matrix from a list of matrices to an unlisted 2D numeric/matrix without flattening to a vector in R? 一致地将子集矩阵转换为向量并避免使用colnames? - Consistently subset matrix to a vector and avoid colnames?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM