如何在Matlab中有效/方便地计算许多向量的A-内积(即双线性形式)?

时间:2015-07-09 00:13:46

标签: matlab vectorization

我想在Matlab中计算一些像v'*M*v这样的东西,其中v来自给定矩阵A的列,它是正方形且可能很大。即。 v=A(:,j)

最方便,计算效率最高的方法是什么? 我正在考虑使用bsxfun和可能reshape,但不确定它将如何运作。

我记得很久以前读过一篇类似的帖子。但我真的无法找到它。

1 个答案:

答案 0 :(得分:2)

我提出的最快的方法是for循环,而不是最优雅的方式。也许其他人会想到更好的东西。

function Compare(s, v)
M = rand(s);
A = rand(s, v);

%Method 1: for loop
tic
r1 = zeros(1, size(A,2));
for i = 1:size(A,2)
    r1(i) = A(:,i)'*M*A(:,i);
end
dt = toc;
disp(['for loop with ', num2str(size(A,2)), ' vectors of length ', num2str(size(A,1)), ' was ', num2str(dt), ' s.'])

%Method 2: cell functions with anonymous function
tic
Ap = num2cell(A, 1);
r2 = cell2mat(cellfun(@(x) x'*M*x, Ap, 'uni', 0));
dt = toc;
disp(['cell functions using an anonymous function with ', num2str(size(A,2)), ' vectors of length ', num2str(size(A,1)), ' was ', num2str(dt), ' s.'])

%Method 3: Vector multiplication
tic
r3 = diag(A'*M*A);
dt = toc;
disp(['vector multiplication with ', num2str(size(A,2)), ' vectors of length ', num2str(size(A,1)), ' was ', num2str(dt), ' s.'])

end

编辑:输出

>> Compare(20, 200)
    for loop with 200 vectors of length 20 was 0.0016883 s.
    cell functions using an anonymous function with 200 vectors of length 20 was 0.0079001 s.
    vector multiplication with 200 vectors of length 20 was 0.0035036 s.
>> Compare(20, 400)
    for loop with 400 vectors of length 20 was 0.0035246 s.
    cell functions using an anonymous function with 400 vectors of length 20 was 0.010177 s.
    vector multiplication with 400 vectors of length 20 was 0.0076295 s.
>> Compare(20, 800)
    for loop with 800 vectors of length 20 was 0.0069367 s.
    cell functions using an anonymous function with 800 vectors of length 20 was 0.022697 s.
    vector multiplication with 800 vectors of length 20 was 0.0075474 s.
>> Compare(20, 1600)
    for loop with 1600 vectors of length 20 was 0.013802 s.
    cell functions using an anonymous function with 1600 vectors of length 20 was 0.037844 s.
    vector multiplication with 1600 vectors of length 20 was 0.029591 s.
>> Compare(20, 3200)
    for loop with 3200 vectors of length 20 was 0.026893 s.
    cell functions using an anonymous function with 3200 vectors of length 20 was 0.078213 s.
    vector multiplication with 3200 vectors of length 20 was 0.084117 s.
>> Compare(20, 6400)
    for loop with 6400 vectors of length 20 was 0.053695 s.
    cell functions using an anonymous function with 6400 vectors of length 20 was 0.15759 s.
    vector multiplication with 6400 vectors of length 20 was 0.3524 s.
>> Compare(40, 1600)
    for loop with 1600 vectors of length 40 was 0.01514 s.
    cell functions using an anonymous function with 1600 vectors of length 40 was 0.040556 s.
    vector multiplication with 1600 vectors of length 40 was 0.028335 s.
>> Compare(80, 1600)
    for loop with 1600 vectors of length 80 was 0.022824 s.
    cell functions using an anonymous function with 1600 vectors of length 80 was 0.053713 s.
    vector multiplication with 1600 vectors of length 80 was 0.047412 s.
>> Compare(160, 1600)
    for loop with 1600 vectors of length 160 was 0.045606 s.
    cell functions using an anonymous function with 1600 vectors of length 160 was 0.096006 s.
    vector multiplication with 1600 vectors of length 160 was 0.052472 s.
>> Compare(320, 1600)
    for loop with 1600 vectors of length 320 was 0.074407 s.
    cell functions using an anonymous function with 1600 vectors of length 320 was 0.1386 s.
    vector multiplication with 1600 vectors of length 320 was 0.19317 s.
>> Compare(640, 1600)
    for loop with 1600 vectors of length 640 was 0.21931 s.
    cell functions using an anonymous function with 1600 vectors of length 640 was 0.36021 s.
    vector multiplication with 1600 vectors of length 640 was 0.24102 s.
>> Compare(1280, 1600)
    for loop with 1600 vectors of length 1280 was 1.6893 s.
    cell functions using an anonymous function with 1600 vectors of length 1280 was 1.8245 s.
    vector multiplication with 1600 vectors of length 1280 was 0.57957 s.
>> Compare(2560, 1600)
    for loop with 1600 vectors of length 2560 was 6.8812 s.
    cell functions using an anonymous function with 1600 vectors of length 2560 was 7.0459 s.
    vector multiplication with 1600 vectors of length 2560 was 1.2919 s.