在 MatLab 中将矩阵的列与 3d 矩阵的 2d 矩阵切片相乘

发布于 2024-12-24 21:44:51 字数 304 浏览 2 评论 0原文

基本上,我想执行以下计算:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2024-12-31 21:44:51

尝试使用Matlab文件中的 mtimesx交换。这是迄今为止我发现的执行此类 n 维数组乘法的最佳(快速/高效)工具,因为它使用 mex 。我认为您还可以使用 bsxfun,但是我的 Matlab-fu 不足以完成这类事情。

您有 mxnx kmx k 并且想要生成 nx k

mtimesxixjx kjxrx k 等输入相乘以产生 ixrx k

要将问题以 mtimesx 形式表示,请将 G 设为 mxnx k,并将 S 展开为 nx 1 x k。那么 mtimesx(G,S) 将是 mx 1 x k,然后可以将其展平为 mx k

m=3; 
n=4; 
k=2;
G=rand(m,n,k);
S=rand(n,k);

% reshape S
S2=reshape(S,n,1,k);

% do multiplication and flatten mx1xk to mxk
Ans_mtimesx = reshape(mtimesx(G,S2),m,k)

% try loop method to compare
Answer=zeros(m,k);
for Index=1:k
    Answer(:,Index)=G(:,:,Index)*S(:,Index);
end

% compare
norm(Ans_mtimesx-Answer)
% returns 0.

因此,如果您想要一句俏话,您可以这样做:

Ans = reshape(mtimesx(G,reshape(S,n,1,k)),m,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 use bsxfun, but my Matlab-fu is not enough for this sort of thing.

You have m x n x k and m x k and want to produce a n x k.

mtimesx multiplies inputs like i x j x k and j x r x k to produce i x r x k.

To put your problem in mtimesx form, let G be m x n x k, and expand S to be n x 1 x k. Then mtimesx(G,S) would be m x 1 x k, which could then be flattened down to m x k.

m=3; 
n=4; 
k=2;
G=rand(m,n,k);
S=rand(n,k);

% reshape S
S2=reshape(S,n,1,k);

% do multiplication and flatten mx1xk to mxk
Ans_mtimesx = reshape(mtimesx(G,S2),m,k)

% try loop method to compare
Answer=zeros(m,k);
for Index=1:k
    Answer(:,Index)=G(:,:,Index)*S(:,Index);
end

% compare
norm(Ans_mtimesx-Answer)
% returns 0.

So if you wanted a one-liner, you could do:

Ans = reshape(mtimesx(G,reshape(S,n,1,k)),m,k)

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!

不回头走下去 2024-12-31 21:44:51

这是 bsxfun() 版本。如果 A 是 m×n 矩阵且 x 是 n×1 向量
那么 A*x 可以计算为

sum(bsxfun(@times, A, x'), 2)

操作 permute(S, [3 1 2]) 将采用 S 的列并将它们沿第三维作为行分布。 [3 1 2] 是 S 维度的排列。

因此 sum(bsxfun(@times, G, permute(S, [3 1 2])), 2) 获得了答案,但是
将结果保留在第三维中。为了得到你想要的形式
需要另一个排列。

permute(sum(bsxfun(@times, G, permute(S, [3 1 2])), 2), [1 3 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

sum(bsxfun(@times, A, x'), 2)

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.

permute(sum(bsxfun(@times, G, permute(S, [3 1 2])), 2), [1 3 2])
雪化雨蝶 2024-12-31 21:44:51

您可以做的一件事是将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文