Is it possible to vectorize this loop?
1 回表示 (過去 30 日間)
古いコメントを表示
A = rand(1, 100);
B = rand(1, 100);
w = 0;
for i = 1:length(A)
w = w + A(i).*B;
end
0 件のコメント
採用された回答
その他の回答 (1 件)
Jan
2020 年 11 月 30 日
編集済み: Jan
2020 年 11 月 30 日
A = rand(1, 1000);
B = rand(1, 1000);
tic
for k = 1:1000
w = 0;
for i = 1:length(A)
w = w + A(i).*B;
end
end
toc
tic
for k = 1:1000
w = (sum(A .* B(:),2))';
end
toc
tic
w = 0;
for k = 1:1000
w = sum(A' * B);
end
toc
I get the timings (Matlab online!):
0.34 seconds
0.69 seconds
0.41 seconds
So check it on your machine is the vectorization is an advantage.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!