http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038
MATLAB Central Newsreader  How to select and sum elements in an array fast?
Feed for thread: How to select and sum elements in an array fast?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 16 Jul 2010 02:40:08 +0000
How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763045
Namo Namo
I have two vectors  one contains the values, and the other is the corresponding index. For example<br>
<br>
A = [0.1 0.3 0.2 0.5 .... ]<br>
B = [1 3 4 2 1 ]<br>
<br>
The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc.<br>
<br>
I can do a for loop<br>
<br>
S = zeros(length(B),1);<br>
for k =1:length(B)<br>
S(k) = sum( A(B==k) );<br>
end<br>
<br>
But for large A and B, this takes too long. Any advice is welcome.

Fri, 16 Jul 2010 02:51:04 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763047
Roger Stafford
"Namo Namo" <wynamo@yahoo.com> wrote in message <i1ogq8$9j7$1@fred.mathworks.com>...<br>
> I have two vectors  one contains the values, and the other is the corresponding index. For example<br>
> <br>
> A = [0.1 0.3 0.2 0.5 .... ]<br>
> B = [1 3 4 2 1 ]<br>
> <br>
> The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc.<br>
> <br>
> I can do a for loop<br>
> <br>
> S = zeros(length(B),1);<br>
> for k =1:length(B)<br>
> S(k) = sum( A(B==k) );<br>
> end<br>
> <br>
> But for large A and B, this takes too long. Any advice is welcome.<br>
       <br>
S = sum(A(B));<br>
<br>
Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A).<br>
<br>
Roger Stafford

Fri, 16 Jul 2010 03:04:43 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763050
Walter Roberson
Namo Namo wrote:<br>
> I have two vectors  one contains the values, and the other is the <br>
> corresponding index. For example<br>
> <br>
> A = [0.1 0.3 0.2 0.5 .... ]<br>
> B = [1 3 4 2 1 ]<br>
> <br>
> The have the same length. I need to select the elements in A with the <br>
> same index in B, and sum them.<br>
<br>
Consider using accumarray()

Fri, 16 Jul 2010 03:09:04 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763052
Namo Namo
I just tried this with the simple example<br>
<br>
>> A = [0.1 0.3 0.2 0.5 0.2 0.9];<br>
>> B = [1 3 4 2 1 4 ];<br>
>> sum(A(B))<br>
ans =<br>
1.7000<br>
<br>
This is not what I want. I may have made a typo in my previous post. What I want is to sum all A elements corresponding to 1 in B, 2 in B, etc. So here since there are 1 to 4 in B, I expect to get 4 numbers<br>
<br>
sum(A(B==1)) ... sum(A(B==4))<br>
<br>
for example, A(B==4) = [0.2 0.9], and the sum is 1.1 <br>
<br>
>        <br>
> S = sum(A(B));<br>
> <br>
> Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A).<br>
> <br>
> Roger Stafford

Fri, 16 Jul 2010 03:09:04 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763053
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1ohen$jid$1@fred.mathworks.com>...<br>
> "Namo Namo" <wynamo@yahoo.com> wrote in message <i1ogq8$9j7$1@fred.mathworks.com>...<br>
> > I have two vectors  one contains the values, and the other is the corresponding index. For example<br>
> > <br>
> > A = [0.1 0.3 0.2 0.5 .... ]<br>
> > B = [1 3 4 2 1 ]<br>
> > <br>
> > The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc.<br>
> > <br>
> > I can do a for loop<br>
> > <br>
> > S = zeros(length(B),1);<br>
> > for k =1:length(B)<br>
> > S(k) = sum( A(B==k) );<br>
> > end<br>
> > <br>
> > But for large A and B, this takes too long. Any advice is welcome.<br>
>        <br>
> S = sum(A(B));<br>
> <br>
> Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A).<br>
> <br>
> Roger Stafford<br>
       <br>
I didn't read your code carefully enough, Namo. Forget what I sent earlier. The following might do the job:<br>
<br>
S = accumarray(B,A(B));<br>
<br>
Roger Stafford

Fri, 16 Jul 2010 03:26:03 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#763056
Namo Namo
Thanks, Walter and Roger. accumarray(B, A) does it!. It sums the elements in A that have the same index in B.

Mon, 14 Apr 2014 06:23:11 +0000
Re: How to select and sum elements in an array fast?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287038#921212
Yuji Zhang
Hi I'm new to this function.<br>
I think you should say: it sum the elements in A that are appointed by B. For elements of same value in B, we sum up corresponding elements in A. And the value of the elements in B specifies the index in the result.<br>
<br>
<br>
"Namo Namo" wrote in message <i1ojgb$s8s$1@fred.mathworks.com>...<br>
> Thanks, Walter and Roger. accumarray(B, A) does it!. It sums the elements in A that have the same index in B.