Using a loop to group data and find a Frequency

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

Thank you, so how do I apply that to my actual data?
my matlab is below
year = earthquakecatalogue(:,1); %Obtaining the years from the array
magnitudeall = earthquakecatalogue(:,10); %obtaining the earthquakes
Magnitude = [magnitudeall,year]; %putting all the earthquakes and years together in one array
Mag7 = find(Magnitude(:,1) >=7); % finding the eathquakes equally 7 and above
Year7 = Magnitude(Mag7,2); %finding the years correlating to the 7 above magnitudes
%so as this is my actual data and I dont have fake data which parts do I need to replace in your example? do I just replace repelem(1960:2016,1,5) with Year7,Mag7? Or is there something else? I suppose I need to convert the Mag7 into the actual data as well instead of row numbers?
%Thank you for your help!
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));
@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.

Sign in to comment.

Categories

Find more on Seismology in Help Center and File Exchange

Asked:

on 6 Mar 2019

Commented:

on 19 Sep 2022

Community Treasure Hunt

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

Start Hunting!