Announcements | Syllabus | Schedule | Weekly lecture notes | Assignments | Links |
Today's lecture will focus on some tools that you can use to explore your dataset a little more closely. Along the way, we will teach you two important concepts that we use heavily in data analysis. First we will go over some syntax that will help you construct some special arrays very quickly. Then we will introduce the find function in Matlab that helps you search for values in your data. For the remaining class time, you will be able to use these tools to create several different kinds of plots in Matlab.
For the examples in this demo, we will refer to Yellowstone seismicity. At the end of 2008, Yellowstone National Park experienced an increase in seismic activity in the Yellowstone Lake area. We will take a look at this swarm of earthquakes, as well as others that have been recorded since 1973.
You can find out more about this swarm here.
You will need to download the text file that includes the earthquake data from here.
The columns in the text file are as follows:
YEAR YYYYMMDDHHMMSS.SS LATITUDE LONGITUDE DEPTH MAGNITUDE
>> Years = [1973 1974 1975 1976 1977 1978 1979 ... 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 ... 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 ... 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009];Or, you can define a vector by using the following syntax:
>> Years = [1973:1:2009];But what if you don't know the minimum and maximum values to use? Here is how I might make my vector by using the min and max functions:
>> MinYear = min(ystone_eqs_sort(:,1)); >> MaxYear = max(ystone_eqs_sort(:,1)); >> Years = [MinYear:1:MaxYear];Even better, you can insert the min and max functions directly into the vector creation line:
>> Years = [min(ystone_eqs_sort(:,1)):1:max(ystone_eqs_sort(:,1))];
>> ones(3) ans = 1 1 1 1 1 1 1 1 1A Square Matrix with all 0's
>> zeros(3) ans = 0 0 0 0 0 0 0 0 0To make arrays with n rows and m columns, the syntax would be ones(n,m) and zeros(n,m).
>> 17.2 .* ones(4,1) ans = 17.2 17.2 17.2 17.2
>> A=[1 5 0; 0 9 5] A = 1 5 0 0 9 5 >> tf = A > 5 tf = 0 0 0 0 1 0In the above example, tf is a new matrix filled with 1's and 0's. There is a 1 where the conditional of A > 5 was true, and 0 where it was false.
>> tf = A >= 5 tf = 0 1 0 0 1 1or
>> tf = A < 5 tf = 1 0 1 1 0 0 >> tf = A == 5 tf = 0 1 0 0 0 1Note in the latter case, the use of the == to mean the test if they are equal and how that varies from the single = which is the assignment of the result to the tf variable. Let's go back to:
>> A A = 1 5 0 0 9 5 >> tf = A > 5 tf = 0 0 0 0 1 0Now here is where things get to be really neat:
>> locs = find(tf) locs = 4 >> A(locs) ans = 9Look at the MATLAB documentation for find. Basically it returns the array locations where values are == 1. In combination with the conditional or boolean statement like the tf examples above, it is very powerful for processing data.
>> tf=ystone_eqs_sort(:,1)==2008; >> locs=find(tf)Now let's plot them:
>> figure(1) >> clf >> plot(ystone_eqs_sort(:,4),ystone_eqs_sort(:,3), 'k.') >> hold on >> plot(ystone_eqs_sort(locs,4),ystone_eqs_sort(locs,3), 'ro') >> xlabel('longitude') >> ylabel('latitude') >> title('Yellowstone seismicity (2008 in red)')
>> plot(ystone_eqs_sort(:,4),ystone_eqs_sort(:,3), 'k.') >> plot(ystone_eqs_sort(locs,4),ystone_eqs_sort(locs,3), 'ro')the second plot command would "blow away" the first earthquakes that we plotted. So hold on, tells Matlab to "hold on! We've got some more stuff to plot here!" To then release the figure, so that the next plot command will replace the figure, we can type hold off.