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 sum elements with identical subscript

Subject: how to sum elements with identical subscript

From: vagrom Jiang

Date: 29 Nov, 2008 02:32:01

Message: 1 of 4

here is an example, A is N*2 matrix,
A = [2 20;
        5 10;
        5 30;
        7 10;
        .......];
expected result is A= [2 20;
                                5 40;
                                7 10;
                                .........];
how to solve this problem without any Loop to implement, since N could be very large, e.g 10e6.



 

Subject: how to sum elements with identical subscript

From: John D'Errico

Date: 29 Nov, 2008 02:40:18

Message: 2 of 4

"vagrom Jiang" <vagrom@mytum.de> wrote in message <ggq9j1$ssm$1@fred.mathworks.com>...
> here is an example, A is N*2 matrix,
> A = [2 20;
> 5 10;
> 5 30;
> 7 10;
> .......];
> expected result is A= [2 20;
> 5 40;
> 7 10;
> .........];
> how to solve this problem without any Loop to implement, since N could be very large, e.g 10e6.
>

Consolidator does this. Find it here:

http://www.mathworks.com/matlabcentral/fileexchange/8354

HTH,
John

Subject: how to sum elements with identical subscript

From: vagrom Jiang

Date: 29 Nov, 2008 02:57:01

Message: 3 of 4

That is so great! thanks a lot!

Subject: how to sum elements with identical subscript

From: Roger Stafford

Date: 29 Nov, 2008 04:56:01

Message: 4 of 4

"vagrom Jiang" <vagrom@mytum.de> wrote in message <ggq9j1$ssm$1@fred.mathworks.com>...
> here is an example, A is N*2 matrix,
> A = [2 20;
> 5 10;
> 5 30;
> 7 10;
> .......];
> expected result is A= [2 20;
> 5 40;
> 7 10;
> .........];
> how to solve this problem without any Loop to implement, since N could be very large, e.g 10e6.

  Or if you want to do it from scratch you can do it this way:

 [t,m,n] = unique(A(:,1));
 A = [t,accumarray(n,A(:,2))];
 
Roger Stafford

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