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:
problem with a matrix

Subject: problem with a matrix

From: Konst

Date: 5 May, 2012 18:57:07

Message: 1 of 7

I have a matrix (i,2) eg
table=1 2
         1 4
         1 5
         2 8
etc.
I want somehow to search the whole table and while the elements of the first column are the same, sum the elements of the second column till then and put them in a vector.
So here I should have vector=(11 8...)
Any ideas how to do this?I tried with "while" but it doesn't return the correct results.

Subject: problem with a matrix

From: John D'Errico

Date: 5 May, 2012 19:07:07

Message: 2 of 7

"Konst " <konstance1@hotmail.com> wrote in message <jo3t63$3c8$1@newscl01ah.mathworks.com>...
> I have a matrix (i,2) eg
> table=1 2
> 1 4
> 1 5
> 2 8
> etc.
> I want somehow to search the whole table and while the elements of the first column are the same, sum the elements of the second column till then and put them in a vector.
> So here I should have vector=(11 8...)
> Any ideas how to do this?I tried with "while" but it doesn't return the correct results.

consolidator is on the file exchange. It can do a variety
of things beyond what you ask.

[uniquecol1,col2sums] = consolidator(table(:,1),table(:,2),@sum);

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

John

Subject: problem with a matrix

From: Nasser M. Abbasi

Date: 5 May, 2012 19:13:18

Message: 3 of 7

On 5/5/2012 1:57 PM, Konst wrote:
> I have a matrix (i,2) eg
> table=1 2
> 1 4
> 1 5
> 2 8
> etc.
> I want somehow to search the whole table and while the elements of the first
>column are the same, sum the elements of the second column till then and put them in a vector.
> So here I should have vector=(11 8...)
> Any ideas how to do this?I tried with "while" but it doesn't return the correct results.

may be one way

-------------------------
I = unique(A(:,1))
arrayfun( @(i) sum(A(A(:,1)==I(i),2)), 1:length(I))
---------------------------

ans =

     11 8

--Nasser

Subject: problem with a matrix

From: Konst

Date: 5 May, 2012 19:38:20

Message: 4 of 7

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jo3u4p$s98$2@speranza.aioe.org>...
> On 5/5/2012 1:57 PM, Konst wrote:
> > I have a matrix (i,2) eg
> > table=1 2
> > 1 4
> > 1 5
> > 2 8
> > etc.
> > I want somehow to search the whole table and while the elements of the first
> >column are the same, sum the elements of the second column till then and put them in a vector.
> > So here I should have vector=(11 8...)
> > Any ideas how to do this?I tried with "while" but it doesn't return the correct results.
>
> may be one way
>
> -------------------------
> I = unique(A(:,1))
> arrayfun( @(i) sum(A(A(:,1)==I(i),2)), 1:length(I))
> ---------------------------
>
> ans =
>
> 11 8
>
> --Nasser
Thanks!It works fine!

Subject: problem with a matrix

From: Bruno Luong

Date: 6 May, 2012 05:50:42

Message: 5 of 7

"Nasser M. Abbasi" <nma@12000.org> wrote in message
> I = unique(A(:,1))
> arrayfun( @(i) sum(A(A(:,1)==I(i),2)), 1:length(I))
> ---------------------------

Arrayfun is like for-loop. Further more this code has N^2 complexity or array of size N (thus bad runtime for large array).

Consider using accumarray for large data:

[t1, ~, J] = unique(table(:,1));
[t1 accumarray(J,table(:,2))]

% Bruno

Subject: problem with a matrix

From: Matt J

Date: 6 May, 2012 13:13:07

Message: 6 of 7

Another possibility,

vector=sparse(table(:,1),1,table(:,2));

Subject: problem with a matrix

From: Bruno Luong

Date: 6 May, 2012 13:24:08

Message: 7 of 7

"Matt J" wrote in message <jo5td3$753$1@newscl01ah.mathworks.com>...
> Another possibility,
>
> vector=sparse(table(:,1),1,table(:,2));

With few limitations:

1) something in table(:,1) is negative
2) something table(:,1) is not integer
3) One of the sum becomes zero

Bruno

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