如何 efficiently/conveniently 在 Matlab 中计算许多向量的 A 内积(即双线性形式)?
how to efficiently/conveniently compute A-inner product (ie. a bilinear form) of many vectors in Matlab?
我想在 Matlab 中计算类似 v'*M*v
的东西,其中 v
取自给定矩阵 A
的列,它是方形的并且可能很大。 IE。 v=A(:,j)
执行此操作最方便且计算效率最高的方法是什么?
我正在考虑使用 bsxfun
并可能使用 reshape
但不确定它究竟如何工作。
我记得很久以前读过类似的 post。但是我真的找不到。
我想到的最快的方法是 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
编辑:输出
TL;DR:For 循环对于小数据量更快,向量乘法对于大数据量更快。
>> 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.
我想在 Matlab 中计算类似 v'*M*v
的东西,其中 v
取自给定矩阵 A
的列,它是方形的并且可能很大。 IE。 v=A(:,j)
执行此操作最方便且计算效率最高的方法是什么?
我正在考虑使用 bsxfun
并可能使用 reshape
但不确定它究竟如何工作。
我记得很久以前读过类似的 post。但是我真的找不到。
我想到的最快的方法是 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
编辑:输出
TL;DR:For 循环对于小数据量更快,向量乘法对于大数据量更快。
>> 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.