Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to select and sum elements in an array fast?

Subject: How to select and sum elements in an array fast?

From: Namo Namo

Date: 16 Jul, 2010 02:40:08

Message: 1 of 7

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.

Subject: How to select and sum elements in an array fast?

From: Roger Stafford

Date: 16 Jul, 2010 02:51:04

Message: 2 of 7

"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

Subject: How to select and sum elements in an array fast?

From: Walter Roberson

Date: 16 Jul, 2010 03:04:43

Message: 3 of 7

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

Subject: How to select and sum elements in an array fast?

From: Namo Namo

Date: 16 Jul, 2010 03:09:04

Message: 4 of 7

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

Subject: How to select and sum elements in an array fast?

From: Roger Stafford

Date: 16 Jul, 2010 03:09:04

Message: 5 of 7

"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

Subject: How to select and sum elements in an array fast?

From: Namo Namo

Date: 16 Jul, 2010 03:26:03

Message: 6 of 7

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

Subject: How to select and sum elements in an array fast?

From: Yuji Zhang

Date: 14 Apr, 2014 06:23:11

Message: 7 of 7

Hi I'm new to this function.
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.


"Namo Namo" wrote in message <i1ojgb$s8s$1@fred.mathworks.com>...
> Thanks, Walter and Roger. accumarray(B, A) does it!. It sums the elements in A that have the same index in B.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us