Using an If statement in a For loop to sum quantities from an excel file.
Show older comments
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
Azzi Abdelmalek
on 5 Apr 2016
@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.
Kannan Ridings
on 6 Apr 2016
Azzi Abdelmalek
on 6 Apr 2016
Ok, then look at the answer
Answers (1)
Azzi Abdelmalek
on 5 Apr 2016
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))]
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!