title: Daily maximum 8-Hour running mean/moving average
body: <p>I have hourly data and I want to do find the daily max 8-hour average. Basically, for each hour of the day, I want to do an 8-hour average. So take the average of 0:00 to 8:00, then 1:00 to 9:00, etc.), so I end up with 24 8-hour average periods (with some running into the next day of course). Then I need to take the maximum of those 24 8-hour averages to get the daily max. </p>
  
  <p>The .mat file used can be found here: <a href="https://www.dropbox.com/sh/9e2dgm0imvr0hpe/tAUOtpZEEa" rel="nofollow">https://www.dropbox.com/sh/9e2dgm0imvr0hpe/tAUOtpZEEa</a></p>
  
  <p>A note about the format of the file: The O3.mat file has a variable called O3_Sorted that is a cell array. It contains all data, sorted already. But the data contains information from more than one site (i.e. there is information from different places). The information for each site is sorted together, but in the code, when I try to find the 8-hour averages, I have to pull out one site at a time so that the averages don't run into the beginning of the data for another place. </p>
  
  <p>Here's a sample of what things look like. I included one day for one site and half a day of another site. The actual file has a month of data for each of these sites and other sites as well. As you can see, sometimes, the data is missing. 
  Column 1 - Site name
  Column 2 - Date
  Column 3 - Hour
  Column 4 - Data</p>
  
  <pre><code>003-0010    2007-05-31  00:00   0.016
  003-0010    2007-05-31  01:00   0.015
  003-0010    2007-05-31  02:00   0.002
  003-0010    2007-05-31  03:00   0.03
  003-0010    2007-05-31  04:00   0.019
  003-0010    2007-05-31  05:00   0.013
  003-0010    2007-05-31  06:00   0.018
  003-0010    2007-05-31  07:00   0.024
  003-0010    2007-05-31  08:00   0.031
  003-0010    2007-05-31  09:00   0.029
  003-0010    2007-05-31  10:00   0.031
  003-0010    2007-05-31  11:00   0.035
  003-0010    2007-05-31  12:00   0.026
  003-0010    2007-05-31  13:00   0.026
  003-0010    2007-05-31  14:00   0.033
  003-0010    2007-05-31  15:00   0.039
  003-0010    2007-05-31  16:00   0.036
  003-0010    2007-05-31  17:00   0.035
  003-0010    2007-05-31  18:00   0.031
  003-0010    2007-05-31  19:00   0.03
  003-0010    2007-05-31  20:00   0.03
  003-0010    2007-05-31  21:00   0.017
  003-0010    2007-05-31  22:00   0.017
  003-0010    2007-05-31  23:00   0.007
  027-0007    2007-05-31  00:00   0.045
  027-0007    2007-05-31  01:00   0.043
  027-0007    2007-05-31  02:00   
  027-0007    2007-05-31  03:00   0.038
  027-0007    2007-05-31  04:00   0.037
  027-0007    2007-05-31  05:00   0.034
  027-0007    2007-05-31  06:00   0.034
  027-0007    2007-05-31  07:00   0.038
  027-0007    2007-05-31  08:00   0.044
  027-0007    2007-05-31  09:00   0.05
  027-0007    2007-05-31  10:00   0.054
  027-0007    2007-05-31  11:00   0.051
  027-0007    2007-05-31  12:00   0.047
  </code></pre>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>for i = 1:size(O3_sites)
      I = ismember(D(:,6), O3_sites(i)); % Rows were the cell array O3_sorted has data corresponding to a certain site
      site = D(I,:);
  
      %% Convert O3 from ppm to ppb, 1ppm = 1000ppb
      x = 1000;
      y = str2double(O3);
      O3_data = bsxfun(@times,x,y); % ppb    
  
  % Find size of array
  [M, N]= size(O3_data); 
  
  % Create empty array
  O3_MD8 = zeros(N,M-7); % double
  
  **% Do a loop to calculate the running mean
  for j = 1:M-7
      A = O3_data(j:j+7);
      O3_MD8(:,j) = mean(A);
  end**
  
  % Find max from each 8-hour loop
  
  end
  </code></pre>
  
  <p>After I get the 8-hour averages, how can I ask MATLAB to find the max for each 24 averages? Basically, get the max of the hourly averages. </p>
  
  <p>Also, the method I'm trying to do now is a bit risky because I'm not using datenum and so if data is missing a day, I won't know. But I have no idea how to consider that when writing the code. </p>