title: How should I average groups of rows in a matrix to produce a new, smaller matrix?
body: <p>I have a very large matrix (216 rows, 31286 cols) of doubles. For reasons specific to the data, I want to average every 9 rows to produce one new row. So, the new matrix will have 216/9=24 rows.</p>
  
  <p>I am a Matlab beginner so I was wondering if this solution I came up with can be improved upon. Basically, it loops over every group, sums up the rows, and then divides the new row by 9. Here's a simplified version of what I wrote:</p>
  
  <pre><code>matrix_avg = []
  for group = 1:216/9
      new_row = zeros(1, 31286);
      idx_low = (group - 1) * 9 + 1;
      idx_high = idx_low + 9 - 1;
      % Add the 9 rows to new_row
      for j = idx_low:idx_high
          new_row = new_row + M(j,:);
      end
      % Compute the mean
      new_row = new_row ./ 9
      matrix_avg = [matrix_avg; new_row];
  end
  </code></pre>