Using an If statement in a For loop to sum quantities from an excel file.

I have an excel file with two columns. One with years ranging from 2016 to 2010 (which are all repeated many times), and the other with an integer that corresponds to how many citations a particular paper got in that year. I want to sum all the citations from each year and then put them into a vector. So far when I run my code it just sums that total number of citations for every year and then outputs that into a vector. I want the sum of citations from 2016 as one vector entry, the sum of citations from 2015 as the next vector entry and so on. Here's what I got so far
cite = zeros(7,1);
for ii = 1:length(cite);
for iii = 1:length(A);
for years = 2016:-1:2010;
if A(iii,1) == years
cite(ii) = cite(ii) + sum(A(iii,9));
end
end
end
end
Please help me diagnose my problem.

3 Comments

@Kannan, data are from Excell file or another type of file doesn't matter to resolve your problem. You should post a short example with expected result and explain what you want.
So I would have something like
Year Citations
2016 1
2016 0
2016 5
. .
. .
. .
2015 17
2015 23
. .
. .
. .
2010 97
2010 243
2010 79
etc...Reading the excel file is not a problem. I just want to use the data to create a vector of the form
2016 6
2015 40
. .
. .
. .
2010 419
where the left column is the year, and the right column is the sum of the citations.

Sign in to comment.

Answers (1)

A=[2010 1;2010 3;2011 5;2010 8 ;2013 4 ;2014 9;2016 1;2014 2 ;2014 7;2016 7]
[ii,jj,kk]=unique(A(:,1))
out=[ii accumarray(kk,A(:,2))]

Asked:

on 5 Apr 2016

Commented:

on 6 Apr 2016

Community Treasure Hunt

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

Start Hunting!