COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to select and sum elements in an array fast?

• Follow

```I have two vectors -- one contains the values, and the other is the corresponding index.  For example

A = [0.1 0.3 0.2 0.5 .... ]
B = [1  3  4  2  1 ]

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.

I can do a for loop

S = zeros(length(B),1);
for k =1:length(B)
S(k) = sum( A(B==k) );
end

But for large A and B, this takes too long.  Any advice is welcome.
```
 0

```"Namo Namo" <wynamo@yahoo.com> wrote in message <i1ogq8\$9j7\$1@fred.mathworks.com>...
> I have two vectors -- one contains the values, and the other is the corresponding index.  For example
>
> A = [0.1 0.3 0.2 0.5 .... ]
> B = [1  3  4  2  1 ]
>
> 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.
>
> I can do a for loop
>
> S = zeros(length(B),1);
> for k =1:length(B)
>   S(k) = sum( A(B==k) );
> end
>
> But for large A and B, this takes too long.  Any advice is welcome.
- - - - - - - -
S = sum(A(B));

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).

Roger Stafford
```
 0
Reply ellieandrogerxyzzy (4732) 7/16/2010 2:51:04 AM

```Namo Namo wrote:
> I have two vectors -- one contains the values, and the other is the
> corresponding index.  For example
>
> A = [0.1 0.3 0.2 0.5 .... ]
> B = [1  3  4  2  1 ]
>
> The have the same length.  I need to select the elements in A with the
> same index in B, and sum them.

Consider using accumarray()
```
 0

```"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1ohen\$jid\$1@fred.mathworks.com>...
> "Namo Namo" <wynamo@yahoo.com> wrote in message <i1ogq8\$9j7\$1@fred.mathworks.com>...
> > I have two vectors -- one contains the values, and the other is the corresponding index.  For example
> >
> > A = [0.1 0.3 0.2 0.5 .... ]
> > B = [1  3  4  2  1 ]
> >
> > 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.
> >
> > I can do a for loop
> >
> > S = zeros(length(B),1);
> > for k =1:length(B)
> >   S(k) = sum( A(B==k) );
> > end
> >
> > But for large A and B, this takes too long.  Any advice is welcome.
> - - - - - - - -
>  S = sum(A(B));
>
> 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).
>
> Roger Stafford
- - - - - - -
I didn't read your code carefully enough, Namo.  Forget what I sent earlier.  The following might do the job:

S = accumarray(B,A(B));

Roger Stafford
```
 0
Reply ellieandrogerxyzzy (4732) 7/16/2010 3:09:04 AM

```I just tried this with the simple example

>>  A = [0.1 0.3 0.2 0.5 0.2 0.9];
>>  B = [1 3 4 2 1 4 ];
>> sum(A(B))
ans =
1.7000

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

sum(A(B==1))      ...     sum(A(B==4))

for example,   A(B==4) = [0.2 0.9], and the sum is 1.1

> - - - - - - - -
>  S = sum(A(B));
>
> 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).
>
> Roger Stafford
```
 0
Reply wynamo (9) 7/16/2010 3:09:04 AM

```Thanks, Walter and Roger.  accumarray(B, A) does it!.  It sums the elements in A that have the same index in B.
```
 0

5 Replies
289 Views

Similiar Articles:

7/24/2012 5:11:04 AM