How to sum up specific columns based on the values in a different column
Show older comments
Matlab coding. So basicially, I have an excel sheet with data that spans 9 years, and for every year, there is data for the 12 months. So the data looks like this:
2022 12 data data data
2022 12 data data data
2022 11 data data data
2022 11 data data data
2022 10 data data data
I want my code to sum all the data within columns 3:16 of the excel sheet for the rows marked a certain month, and then add that sum into one column of a new vector. All data within all years marked by month 12 will be in col 1, all data within all years marked by month 11 in col 2, etc until I have a 1x12 vector which holds the sum of that month in each column
I tried a while loop but my "sumbymonth" vector never produces anything
monthcounter = 12;
colcounter = 1;
while monthcounter >= 1
SumByMonth = sum(yeardata(yeardata(:,3:16)==monthcounter,:);
colcounter = colcounter+1;
monthcounter=monthcounter-1;
end
thank you for your help everyone
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!