本文介绍了使用Matlab/Octave对两个矩阵的核心对应列的外部乘积之和进行矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有两个矩阵A
和B
,它们由列向量组成,如下所示.
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];
有什么方法可以将A
中每一列的外部乘积之和与B中的相应列向量化.这是我的非向量化解决方案.
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
任何帮助将不胜感激.
推荐答案
您不清楚N是什么,但是我假设N =列向量的数量-这意味着您只是在做A * B'
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
这篇关于使用Matlab/Octave对两个矩阵的核心对应列的外部乘积之和进行矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!