# Determining the total value of the number in each position of a matrix that corresponds to another same length matrix

2 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Stephen23
on 3 May 2018

>> year = [2007;2009;2011;2018;2007;2007];

>> medals = [1;1;5;7;3;1];

>> [uni,~,idx] = unique(year);

>> cnt = accumarray(idx,medals);

>> [uni,cnt]

ans =

2007 5

2009 1

2011 5

2018 7

##### 6 Comments

Stephen23
on 6 May 2018

"...so did I misunderstood the question or the answer is wrong?"

Your question gives an example "Eg: in 2007... the total medals i won is 1+3+1", and that is exactly what my code gives you: for 2007 it returns the value 5. The other years seem to be correct too, when I check them. What are you expecting to get?

### More Answers (1)

KSSV
on 3 May 2018

medals = [1;1;5;7;3;1] ;

year = [2007;2009;2011;2018;2007;2007] ;

[c,ia,ib] = unique(year) ;

iwant=sum(medals(ib==1))

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!