Info

この質問は閉じられています。 編集または回答するには再度開いてください。

quasi-cumsum by matrix indexes

1 回表示 (過去 30 日間)
darius
darius 2011 年 12 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Here's an example of what I'm trying to do:
indexes = randi(4,1000,10);
K = rand(4,6);
Q = zeros(1000,10+6);
for k = 1:10
Q(:,k:k+5) = Q(:,k:k+5) + K(indexes(:,k),:);
end
Any idea if this is vectorizable? THANKS!

回答 (2 件)

Sean de Wolski
Sean de Wolski 2011 年 12 月 19 日
Why bother?
That loop takes less than seven 10,000ths of a second on my system. You could run this loop billions of times in the time it might take you to possibly gain a 10000th of a second by vectorizing.
More
Even more reason to not vectorize since memory requirements will be huge and slow it down more than a for-loop.
Your numbers didn't scale up but here's the loop with comparable numbers run the same amount of times:
indexes = randi(100,3000,150);
K = rand(3000,47);
Q = zeros(3000,1000+46);
tic
for jj = 1:10
for k = 1:100
Q(:,k:k+46) = Q(:,k:k+46) + K(indexes(:,k),:);
end
end
toc
Elapsed time is 1.330083 seconds.
I would guess any vectorization, if possible, would be significantly slower due to the memory requirements.

darius
darius 2011 年 12 月 19 日
Hi Sean, thanks for your answer. Note the numbers are just for illustration. Replace 4,1000,10, and 6 with 100, 3000, 150, and 47, then loop the loop 1000 times, and you'll see why vectorization might be helpful.

この質問は閉じられています。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by