title: Function to split a matrix into sub-matrices in R
body: <p>I have a matrix M with 16 rows and 12 columns and I want to split it into a array of 16 matrices, each with 4 rows and 3 columns. I can do it manually by: </p>
  
  <pre><code>M = matrix(sample(0:127,16*12,replace=TRUE), c(16,12))
  
  ma1 = M[1:4,1:3]
  ma2 = M[1:4,4:6]
  ma3 = M[1:4,7:9]
  ma4 = M[1:4,10:12]
  
  ma5 = M[5:8,1:3]
  ma6 = M[5:8,4:6]
  .....
  </code></pre>
  
  <p>But how can I create a generic function matsplitter(M, r, c) which splits M into an array of matrices, each with r rows and c columns?</p>
  
  <p>Thanks for your help. </p>