在 MatLab 中将矩阵的列与 3d 矩阵的 2d 矩阵切片相乘
基本上,我想执行以下计算:
G is m x n x k
S is n x k
Answer=zeros(m,d)
for Index=1:k
Answer(:,Index)=G(:,:,Index)*S(:,Index)
end
所以,答案是一个矩阵,其列是 3d 矩阵的每一层与另一个矩阵的列相乘的结果。
这看起来确实是一种简单的操作类型,我希望找出在 Matlab 中是否有本地或矢量化(或至少>>更快)的方法来执行此类计算。 谢谢。
Basically, I want to perform the following computation:
G is m x n x k
S is n x k
Answer=zeros(m,d)
for Index=1:k
Answer(:,Index)=G(:,:,Index)*S(:,Index)
end
So, answer is a matrix, whose columns are the result of multiplying each layer of a 3d matrix with a column of another matrix.
This really seems like a straightforward type of operation, and I was hoping to find out if there is a native or vectorized (or at least >> faster) way of performing this type of computation in Matlab.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用Matlab文件中的 mtimesx交换。这是迄今为止我发现的执行此类 n 维数组乘法的最佳(快速/高效)工具,因为它使用
mex
。我认为您还可以使用bsxfun
,但是我的 Matlab-fu 不足以完成这类事情。您有
mxnx k
和mx k
并且想要生成nx k
。mtimesx
将ixjx k
和jxrx k
等输入相乘以产生ixrx k
。要将问题以
mtimesx
形式表示,请将G
设为mxnx k
,并将S
展开为nx 1 x k。那么
mtimesx(G,S)
将是mx 1 x k
,然后可以将其展平为mx k
。因此,如果您想要一句俏话,您可以这样做:
顺便说一下,如果您将问题发布在 Matlab 新闻阅读器论坛 将会有很多专家竞相为您提供比我更优雅或更高效的答案!
Try using mtimesx from the Matlab File Exchange. It's the best (fast/efficient) tool I've found so far to do this sort of n-dimensional array multiplication, since it uses
mex
. I think you could also usebsxfun
, but my Matlab-fu is not enough for this sort of thing.You have
m x n x k
andm x k
and want to produce an x k
.mtimesx
multiplies inputs likei x j x k
andj x r x k
to producei x r x k
.To put your problem in
mtimesx
form, letG
bem x n x k
, and expandS
to ben x 1 x k
. Thenmtimesx(G,S)
would bem x 1 x k
, which could then be flattened down tom x k
.So if you wanted a one-liner, you could do:
By the way, if you post your question on the Matlab Newsreader forums there'll be plenty of gurus who compete to give you answers more elegant or efficient than mine!
这是 bsxfun() 版本。如果 A 是 m×n 矩阵且 x 是 n×1 向量
那么 A*x 可以计算为
操作 permute(S, [3 1 2]) 将采用 S 的列并将它们沿第三维作为行分布。 [3 1 2] 是 S 维度的排列。
因此 sum(bsxfun(@times, G, permute(S, [3 1 2])), 2) 获得了答案,但是
将结果保留在第三维中。为了得到你想要的形式
需要另一个排列。
Here is the bsxfun() version. If A is an m-by-n matrix and x is an n-by-1 vector
then A*x can be computed as
The operation permute(S, [3 1 2]) will take the columns of S and distribute them along the 3rd dimension as rows. The [3 1 2] is a permutation of the dimensions of S.
Thus sum(bsxfun(@times, G, permute(S, [3 1 2])), 2) achieves the answer but
leaves the result in the 3rd dimension. In order to get it in the form you want
another permute is required.
您可以做的一件事是将 3d 矩阵表示为 2d 块对角矩阵,每一层都是对角块。在这种情况下,二维矩阵应表示为包含堆叠列的向量。如果矩阵很大,则将其声明为稀疏矩阵。
One thing you can do is to represent your 3d matrix as a 2d block diagonal matrix, with each layer being a diagonal block. The 2d matrix in this case should be represented as a vector containing the stacked columns. if the matrix is large, declare it as a sparse matrix.