简体   繁体   English

如何将矩阵A的每一列乘以矩阵B的每一行,并在Matlab中求和矩阵?

[英]How to multiply each column of matrix A by each row of matrix B and sum resulting matrices in Matlab?

I have a problem which I hope can be easily solved. 我有一个问题,我希望可以轻松解决。 A is a N G matrix, B is N G matrix. A是N G矩阵,B是N G矩阵。 The goal is to get matrix C 目标是获得矩阵C.

在此输入图像描述

which is equal to multiplying each column of transposed A by each row of B and summing resulting matrices; 这等于将每列转置的A乘以B的每一行并求和得到的矩阵; total number of such matrices before summing is N N, their size is G G This can be easily done in MatLab with two for -loops: 求和之前这样的矩阵的总数是N N,它们的大小是G G。这可以在MatLab中用两个for -loops轻松完成:

N=5;
G=10;
A=rand(N,G);
B=rand(N,G);
C=zeros(G);
for n=1:1:N
    for m=1:1:N
        C=C+A(m,:)'*B(n,:);
    end
end

However, for large matrices it is quite slow. 但是,对于大型矩阵,它非常慢。

So, my question is: is there a more efficient way for calculating C matrix in Matlab? 所以,我的问题是:在Matlab中有更有效的计算C矩阵的方法吗?

Thank you 谢谢

If you write it all out for two 3×3 matrices, you'll find that the operation basically equals this: 如果你把它全部写成两个3×3矩阵,你会发现操作基本上等于这个:

C = bsxfun(@times, sum(B), sum(A).');

Running each of the answers here for N=50 , G=100 and repeating each method 100 times: 在这里运行每个答案N=50G=100并重复每个方法100次:

Elapsed time is 13.839893 seconds. %// OP's original method
Elapsed time is 19.773445 seconds. %// Luis' method
Elapsed time is 0.306447 seconds.  %// Robert's method
Elapsed time is 0.005036 seconds.  %// Rody's method

(a factor of ≈ 4000 between the fastest and slowest method...) (最快和最慢的方法之间≈4000的因子......)

I think this should improve the performance significantly 我认为这应该会显着提高性能

C = zeros(G);
for n = 1:N
   C = C + sum(A,1)'*B(n,:);
end

You avoid one loop, and should also avoid the problems of running out of memory. 您可以避免一个循环,并且还应该避免内存不足的问题。 According to my benchmarking, it's about 20 times faster than the approach with two loops. 根据我的基准测试,它比使用两个循环的方法快20倍。 (Note, I had to benchmark in Octace since I don't have MATLAB on this PC). (注意,我必须在Octace中进行基准测试,因为我在这台PC上没有MATLAB)。

使用bsxfun代替循环,然后sum两次:

C = sum(sum(bsxfun(@times, permute(A, [2 3 1]), permute(B,[3 2 4 1])), 3), 4);

暂无
暂无

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

相关问题 通过3-d矩阵的每个切片将2-d矩阵的每列相乘的更有效方式 - More efficient way to multiply each column of a 2-d matrix by each slice of a 3-d matrix 如何有效地将矩阵的每一行与R中列表的每个部分进行比较? - How to efficiently compare each row of a matrix to each section of a list in R? Matlab以矩阵和单元位置作为参数执行每个单元矩阵的功能 - Matlab execute function each cell Matrix with matrix and cell postion as arguments 我可以在Matlab矩阵的每一行和没有循环的向量之间找到共同的值吗? - Can I find common values between each row of a matlab matrix and a vector without a loop? 根据按列名称分组的列计算矩阵每一行的汇总统计量 - Calculate summary statistics for each row of a matrix based on columns grouped by column names 在矩阵的每一行上应用函数的最快方法是什么? - What is the fastest way to apply a function on each row of a matrix? 从向量创建矩阵,其中每行是向量的移位版本 - Create a matrix from a vector where each row is a shifted version of the vector 如何删除矩阵A的那些行,它们与Matlab中指定列中的矩阵B具有相等的值? - How to remove those rows of matrix A, which have equal values with matrix B in specified columns in Matlab? 在每个循环中更新一个 Numpy 一维数组 B 以求解矩阵表达式 A*x = B - Update a Numpy 1D array B in each loop to solve the matrix expression A*x = B 计算R中矩阵中每个唯一列的出现的最快方法 - Fastest way to count the occurrences of each unique column in a matrix in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM