Vectorize the sum of outer products of coresponding columns of two matrices using Matlab/Octave

本小妞迷上赌 提交于 2019-12-10 22:45:03

问题


Suppose I have two matrices A and B which are made up of column vectors as follows.

A = [a_1,a_2,...,a_N];
B = [b_1,b_2,...,b_N];

Is there any way to vectorize the calculation of the sum of outer products for every column in A with the corresponding column in B. Here is my non-vectorized solution.

S = zeros(size(A,1), size(B,1));
for n=1:N
    S = S + A(:,n)*B(:,n)';   % S = S + a_n * b_n'
end

Any help would be greatly appreciated.


回答1:


you are not clear on what N is, but I assume that N = number of column vectors - which means you are simply doing A * B'

A = rand(3,4);
B = rand(3,4);
N = size(A,2);
S = zeros(size(A,1), size(B,1));
for n=1:N
  S = S + A(:,n)*B(:,n)';   % S = S + a_n * b_n'
end
%Check that you are doing A*B'
S == A*B'
>> ans =

 1     1     1
 1     1     1
 1     1     1


来源:https://stackoverflow.com/questions/43731361/vectorize-the-sum-of-outer-products-of-coresponding-columns-of-two-matrices-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!