下面的循环可以矢量化吗?循环中的每次迭代都会形成一个外积,然后将结果对称化并存储为矩阵中的一列。预计m
大(例如,1e4)和s
小(例如,10)。
% U and V are m-by-s matrices
A = zeros(s^2, m); % preallocate
for k = 1:m
Ak = U(k,:)' * V(k,:);
Ak = (Ak + Ak')/2;
A(:, k) = Ak(:);
end
编辑
以下是 3 种不同方法的比较:迭代大维度m
、迭代小维度s
和bsxfun
基于解决方案(公认且最快的答案)。
s = 5; m = 100000;
U = rand(m, s);
V = rand(m, s);
% Iterate over large dimension
tic
B = zeros(s^2, m);
for k = 1:m
Ak = U(k,:)' * V(k,:);
Ak = (Ak + Ak')/2;
B(:, k) = Ak(:);
end
toc
% Iterate over small dimension
tic
A = zeros(s, s, m);
for i = 1:s
A(i,i,:) = U(:, i) .* V(:, i);
for j = i+1:s
A(i,j,:) = (U(:,i).*V(:,j) + U(:, j).*V(:, i))/2;
A(j,i,:) = A(i,j,:);
end
end
A = reshape(A, [s^2, m]);
toc
% bsxfun-based solution
tic
A = bsxfun( @times, permute( U, [1 3 2] ), permute( V, [ 1 2 3 ] ) );
A = .5 * ( A + permute( A, [1 3 2] ) );
B = reshape( A, [m, s^2] )';
toc
下面是时间对比:
Elapsed time is 0.547053 seconds.
Elapsed time is 0.042639 seconds.
Elapsed time is 0.039296 seconds.