使用Matlab/Octave向量化两个矩阵对应列的外积之和

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

假设我有两个矩阵AB,它们由列向量组成,如下所示。

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

有什么方法可以将 A 中每一列的外积总和的计算与 B 中的相应列向量化。这是我的非向量化解决方案。

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

如有任何帮助,我们将不胜感激。

您不清楚 N 是什么,但我假设 N = 列向量的数量 - 这意味着您只是在做 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