Using a loop to group data and find a Frequency
Show older comments
So i have a set of data, one column of years from 1960 to 2016 and the other earthquake magnitudes. I need to somehow calculate how many earthquakes each year had an earthquake over 7. So ultimately i want to end up with an array which says
1960 5
1961 6
1962 10
etc.
I used the find function to find the values which are over and equal to 7 magnitude. But how do I create a loop to get the frequency of those 7 magnitudes in each year? I dont understand loops at all, I just got told it was the best way to do it, but I'm open to other methods!
Thanks in advance.
Answers (1)
Use splitapply()
No loop needed (preferred method).
% Create fake data: Column 1 are years, column 2 are number of earthquakes
data = [repelem(1960:2016, 1,5)', randi(5,285, 1)-1];
% count number of quakes per year
[groups, years] = findgroups(data(:,1)); %converts years to group numbers
quakeCount = splitapply(@sum, data(:,2), groups); %applys sum() to col 2 for each year-group
% Put results in a table
table(years, quakeCount)
Use a loop
if you must...
% Create fake data: Column 1 are years, column 2 are number of earthquakes
data = [repelem(1960:2016, 1,5)', randi(5,285, 1)-1];
% list unique years
years = unique(data(:,1));
% Loop through each year and count the quakes
quakeCount = zeros(size(years)); %allocate storage variable
for i = 1:length(years)
quakeCount(i) = sum(data(data(:,1)==years(i),2));
end
% Put results in a table
table(years, quakeCount)
4 Comments
Ellen James
on 7 Mar 2019
I suggest you look at each line of my code, execute each line from the command window, and look at the results. From my example, 'data' is a [m x 2] matrix where column 1 are years and column 2 are numbers of earthquakes. That's the only input.
It looks like you have a larger matrix of mixed data and you're pulling the years from column 1 and the magnitudes from column 10. Then you're finding all rows that have magnitudes greater or equal to 7.
You can either 1) create a new matrix of years and earthquakes that only contains the rows you identified as accepted or 2) just use your existing matrix.
I don't want to do this for you because then you learn nothing but here's how one line of my solution would look if you choose option 2 above.
Mag7 = earthquakecatalogue(:,10) >= 7;
[groups, years] = findgroups(earthquakecatalogue(Mag7,1));
Thiago de Aquino Costa Sousa
on 19 Sep 2022
@Adam Danz. I have a table with 60k×87, and I used the same approach as you in Use splitapply(), to combine my variable subject id to the variable trial id. But instead of a math, I would like to plot two others variables (x,y) for the 48 groups created before. And prefearable, 1 one plot for each subject (total 8).
In other words, for each subject I have 6 trials, and I want to create one plot, them apply this to the 7 left participants.
Would that be possible using a for loop in the approach Use splitapply().
Thank you.
Categories
Find more on Seismology 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!