Adding values to a vector

28 views (last 30 days)
Eli
Eli on 30 Jun 2011
Dear Matlab forum, I have a vector, T for which I would like to add values to certain indices in a loop. My simplified code is:
T=zeros(10,1)
for iter=1:100
r=[some indices]; % the indices change each loop and are somewhat random
E=[some values for the indices]; % length(E)==length(r)
T(r)=T(r)+E;
end
The issue I am having is that r may contain a multiple occurrences of given index within a single iteration, for example r=[1 4 2 4], and E could be=[2 2 2 2]. I would like to add BOTH values of E to the index 4, but my above code only adds the last one and ignores the first. What is the most efficient way to solve this issue? Thank you very much, -Eli

Accepted Answer

Matt Fig
Matt Fig on 30 Jun 2011
It has to be done in a loop? You could put a nested loop in there and loop over r...
%
%
EDIT
Here is one way to do it.
T = 1:5;
r = [1 1 1 3 4];
E = [3 3 2 2 5];
T = T + accumarray(r.',E.',[5 1]).'
But it might be slower than using a loop... In fact, since you say that T is 20,000 times longer than r, I would bet this was slower than a loop.
%
%
%
%
EDIT2 The timings report...
Have a look for yourself. Of course some of the accumarray solution could be sped of if the r and E could be made column vectors, which would avoid the transposition, but I doubt that will be much faster...
function [] = accum_loop
T = 1:2e7;
rand('twister',33)
tic
for ii = 1:100
r = ceil(rand(1,1000)*2e7);
E = ceil(rand(1,1000)*10);
for jj = 1:length(r)
T(r(jj)) = T(r(jj)) + E(jj);
end
end
toc
T2 = 1:2e7;
rand('twister',33)
tic
for ii = 1:100
r = ceil(rand(1,1000)*2e7);
E = ceil(rand(1,1000)*10);
T2 = T2 + accumarray(r.',E.',[2e7 1]).' ;
% T2 = T2 + accumarray(r.',E.',[2e7 1],@sum,0,true).' ;
end
toc
isequal(T,T2)
I get, as it stands:
Elapsed time is 0.016339 seconds.
Elapsed time is 7.278608 seconds.
And with your modifications (function handles are slow!):
Elapsed time is 0.016107 seconds.
Elapsed time is 23.642409 seconds.
  13 Comments
Eli
Eli on 1 Jul 2011
Matt,
your above speed test actually brought to my attention that it was another part of my code that was especially time consuming, rather than adding values t T. I will use the loop that you suggested.
The part of my code that needed adjusting was setting the values of r and E. In my code, r and E are selected each iteration from a subset of a much larger two column array (about the length of T), called trac. I was wondering why your time test was so much faster than mine, and it's because of those lines of code. I was able to split trac into a structure of all the r and E vectors I will need before the loop and then just choose them in the loop. This brings my time to around yours. and I'm happy with the speed now.
Thanks for all the help,
-Eli
Matt Fig
Matt Fig on 1 Jul 2011
You're welcome. In the future you should use the profiler to see where the bottleneck in your code is hiding.
help profile
This is an invaluable tool for helping speed up slow code.

Sign in to comment.

More Answers (1)

Jan
Jan on 1 Jul 2011
Try to use a LOGICAL vector for r, which has 2 advantages:
  1. The vector needs less memory such that the allocation is faster
  2. LOGICAL indices do not need a boundary check for each element, such that the copy is faster. The index is applied twice in "T(r)=T(r)+E;"
  1 Comment
Eli
Eli on 1 Jul 2011
Jan, I'm not sure I understand how this would work. r can contain multiple occurrences of given index within a single iteration. Therefore I don't see how I can use a logical vector.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!