Announcements | Syllabus | Schedule | Weekly lecture notes | Assignments | Links |

Sometimes, you need go go through a data set to do some analysis and you cannot do it in the simple vector mode of Matlab. For example,

>> for n = 1:5 n end n = 1 n = 2 n = 3 n = 4 n = 5

I am going to load the data, set up a few variables, and then plot it:

load('ystone_eqs_sort.txt','-ascii'); year = ystone_eqs_sort(:,1); x_locations = ystone_eqs_sort(:,4); y_locations = ystone_eqs_sort(:,3); depth = ystone_eqs_sort(:,5); magnitudes = ystone_eqs_sort(:,6); figure(1) clf plot(x_locations,y_locations,'k.') xlabel('longitude') ylabel('latitude') axis equal hold on

stepsize = 0.1; long_range=min(x_locations):stepsize:max(x_locations); for long = long_range plot(long,min(y_locations),'bo') endHere we need to specify the step size as we move across the grid. Notice the blue circles across the base:

stepsize = 0.1; long_range=min(x_locations):stepsize:max(x_locations); lat_range = min(y_locations):stepsize:max(y_locations); for long = long_range for lat = lat_range plot(long,lat,'bo') end endHere we need to moved across the study area in both the latitude and the longitude directions by the magnitude of the stepsize:

Here it is with a stepsize of 0.05:

stepsize = 0.25; long_range=min(x_locations):stepsize:max(x_locations); lat_range = min(y_locations):stepsize:max(y_locations); for long = long_range for lat = lat_range tf = x_locations <= long+stepsize./2 & x_locations >= long-stepsize./2 & y_locations <= lat+stepsize./2 & y_locations >= lat-stepsize./2; locations = find(tf); mean_depth = mean(depth(locations)); s = sprintf('%2.1f',mean_depth); text(long, lat, s); end endIn this example, I have searched for the depth values within each step (+/- half the step size from the lat long location of the estimate). Then, I computed their mean, converted the mean and some text to a string (

figure(2) plot3(x_locations,y_locations,-depth,'r.') grid on title('Yellowstone Earthquake Hypocenters') xlabel('longitude') ylabel('latitude') zlabel('depth')

The

Repeat the last loop, but lets reference the numbers that we loop through in a little bit different way. We will also build out a matrix of our mean depth values.

stepsize = 0.1; long_range=min(x_locations):stepsize:max(x_locations); lat_range = min(y_locations):stepsize:max(y_locations); % Here we are looping over the range of longitudes for n = 1:length(long_range) % Here we are looping over the range of latitudes for m = 1:length(lat_range) % Notice that we have to index "long_range" by n and "lat_range" by m tf = x_locations <= long_range(n)+stepsize./2 & x_locations >= long_range(n)-stepsize./2 & y_locations <= lat_range(m)+stepsize./2 & y_locations >= lat_range(m)-stepsize./2; locations = find(tf); % Build the "mean_depth" matrix with rows = latitudes, columns = longitudes mean_depth(m,n) = mean(depth(locations)); end endNotice some slight changes to our loop.

Lets plot the values of this matrix using

The syntax is: imagesc(x,y,D)

This is how we would plot our data:

figure(3) clf imagesc(long_range,lat_range,mean_depth) xlabel('longitude') ylabel('latitude') set(gca,'Ydir','normal') colorbar hold on

NOTE: The

We can then plot the earthquakes over the image, along with the mean depth values in each box:

plot(x_locations,y_locations,'k.') for n = 1:length(long_range) for m = 1:length(lat_range) s = sprintf('%2.1f',mean_depth(m,n)); text(long_range(n), lat_range(m), s); end end

FIRST, we need to precondition our x-axis vector (long_range), y-axis vector (lat_range), and matrix (mean_depth). This is because

long_range = long_range - (stepsize./2); lat_range = lat_range - (stepsize./2); long_range_vert = [long_range (long_range(end)+stepsize)]; lat_range_vert = [lat_range (lat_range(end)+stepsize)]; mean_depth(end+1,:) = NaN .* ones(1,size(mean_depth,2)); mean_depth(:,end+1) = NaN .* ones(size(mean_depth,1),1);Now we can plot the matrix by replacing

figure(4) clf pcolor(long_range_vert,lat_range_vert,mean_depth) xlabel('longitude') ylabel('latitude') set(gca,'Ydir','normal') colorbar hold on plot(x_locations,y_locations,'k.')

Last modified: October 6, 2011