title: Matlab: Applying a function to chunks of data
body: <p>I have a Matlab function that takes a matrix and a chunk_size as the input, reshapes it into chunks of chunk_size dimension.</p>
  
  <p>For example, if I have the following matrix:</p>
  
  <pre><code>data=
   1     3
   2     5
   3    42
   4    35
   5    16
   6    48
   7     2
   8    22
   9    20
  10    39
  11    40
  </code></pre>
  
  <p>the function, using a chunk_size of 3 will output:</p>
  
  <pre><code>data(:,:,1) =
   1     3
   2     5
   3    42
  data(:,:,2) =
   4    35
   5    16
   6    48
  data(:,:,3) =
   7     2
   8    22
   9    20
  </code></pre>
  
  <p>The last rows of the original data are discarded because they do not fit completely into a chunk. </p>
  
  <p>I would now be able to let the <strong>user</strong> to specify a function to apply to each slice of the 3D Matrix via a function parameter.
  So, for example, if the user wants to apply a sum of the elements, he could use the sum function provided by matlab to obtain:</p>
  
  <pre><code>data(:,:,1) =
   6    50
  data(:,:,2) =
  15    99
  data(:,:,3) =
  24    44
  </code></pre>
  
  <p>At the same time, I want for example to allow the user to let him apply the sum function on the other dimension, to obtain:</p>
  
  <pre><code>data(:,:,1) =
   4
   7
  45
  data(:,:,2) =
  39
  21
  54
  data(:,:,3) =
   9
  30
  29
  </code></pre>
  
  <p>I feel all of this could be obtained somehow through anonymous functions or something like that, but i can't figure out how.</p>
  
  <p>Any help will be really appreciated.</p>
  
  <p>Thanks</p>