Using loops to find cumulative data

Hi MatLab wizards,
I have an assignment question that I just can't figure out how to complete.
I have a txt file with earthquake data(year month day hour minute second lat long depth magnitude), I have to create an array of all earthquakes of magnitude 7 and higher for each year.
This is what I have so far.
eq_cat=load('usgs_events.txt');
Mag_7=find((eq_cat(:,10)>=7));
Then I need to use find for something similar to this
for i=1920:2020
Mag_7_year=find((eq_cat(:,10)>=7)&(eq_cat(:,1)==i))
But I really don't know where to go next.
Can anyone help?
Thanks
Edit: text file uploaded, careful it's almost 76,000 lines long

 Accepted Answer

Star Strider
Star Strider on 25 Feb 2019
You’re off to a good start. You need to use the unique function and the accumarray function to get the result you want. The only part of this that is not obvious is that you will need to use your ‘Mag_7’ vector for the ‘val’ argument. The rest you can figure out on your own. You will need to use an anonymous function (fourth argument to accumarray) to create the cell array to get the output you want. Coding the anonymous function is straightforward.
There are certainly other ways to do it, however this is likely the most efficient. (I was able to do it in four lines, including the load call.)

4 Comments

Hi, thanks for your help but I've never come across those commands before and find MatLab documentation really obtuse. I'm a complete beginner and this question is holding me back from completing the rest of the paper. Could you break your answer down a bit more please. Explain like I'm five.
Thanks again
If writing the MATLAB code is not the object of your assignment, I’m willing to post my entire solution. (We’re averse to providing solutions to homework problems here, however if writing MATLAB code is a means to and end and not the end itself, then that may not apply.)
You can use accumarray or use a longer and less-efficient loop. The idea is to find every row that corresponds to ‘Mag_7’ for each year, and store it in a cell array for that year (since the number of rows for each year will not be the same).
The loop approach would go something like this:
Mag_7=find((eq_cat(:,10)>=7));
[uYr,~,idx] = unique(eq_cat(Mag_7,1)); % Unique Years & Indices
for k = 1:numel(uYr)
Find ‘idx’ Values Corresponding To Each Value Of ‘k’, Those Correspond To ‘uYr’, So ‘k=1’ Will All Be Magnitude > 7 Earthquakes In ‘uYr(1)’ And So For The Rest
end
That’s the basic idea.
——————
EDIT — (26 Feb 2019 at 00:03)
Assuming that the MATLAB code is the mean to an end and not the end itself, for reference:
eq_cat = load('usgs_events.txt');
Mag_7=find((eq_cat(:,10)>=7));
[uYr,~,idx] = unique(eq_cat(Mag_7,1)); % Unique Years & Corresponding Indices
Quakes = accumarray(idx, Mag_7, [], @(x){eq_cat(x,:)}); % Desired Result
Quakes{1} % View Results (Delete)
Quakes{9} % View Results (Delete)
I trust that you will not use this if writing the MATLAB code is the essence of the assignment, since it would give you an unfair advantage over those who did not request our assistance.
Hi, thanks for your help. Here's what I ended up using.
eq_cat=load('usgs_events.txt');
Mag_7=find(eq_cat(:,10)>=7); % lists all row # with mag 7+
Mag_7_size=length(Mag_7); % 802 instances
m=eq_cat(Mag_7); % Array of earthquakes >=Mag 7
yr=unique(eq_cat(Mag_7)); % each instance of year of Mag7+
Ncount = histc(m, yr); % list of occurences
f=length(Ncount);
eq_final=[yr,Ncount]; % 2 column array(year,count)
That seems to have produced the data I need.
As always, my pleasure.
The histogram functions will certainly work. That’s essentially what accumarray does.
I thought you wanted all the information, not just the counts. My code does the former, although it’s easily possible to change it to get the latter.
I would caution you about the length function. It outputs essentially max(size(x)), and will report the row size if that’s the largest dimension. It’s usually more reliable to use the size function with arrays, and numel with vectors, so you know you’re getting the information you want.

Sign in to comment.

More Answers (0)

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!