Kostas
Kostas

Reputation: 33

How can I sum specific values of a column corresponding to unique values of another column, without using the "accumarray" command?

I have a matrix in matlab:

a=[1 1; 1 2; 1 3; 2 1; 2 5; 2 7; 3 2; 3 1; 3 4];

if

a1=[1 1 1 2 2 2 3 3 3]; is the first column

and

a2=[1 2 3 1 5 7 2 1 4]; is the second column

of this matrix, I want for the repeated values "unique(a1)" of a1 to sum the corresponding values of a2, so as to get this:

a3=[1+2+3 1+5+7 2+1+4]=[6 13 7] 

but without using the "accumarray" command Any help please?

Upvotes: 1

Views: 727

Answers (1)

user85109
user85109

Reputation:

My consolidator tool does this for you, even offering a tolerance.

[a1cons,a2cons] = consolidator(a1',a2',@sum)
a1cons =
     1
     2
     3

a2cons =
     6
    13
     7

Upvotes: 1

Related Questions