title: Pulling subsets from a large 3d matrix using a binary mask - Matlab
body: <p>I've a large <code>D</code> Matrix of size MxNxK. Given a binary mask <code>B</code> of size MxN, I would like to split the matrix <code>D</code> into two submatrices: <code>D0</code> and <code>D1</code>, in such a way that matrix <code>D0</code> has the values of matrix <code>D</code> associated with the <code>0's</code> in the binary mask. The same is valid for <code>D1</code>, but by using the <code>1's</code> in the binary mask.
  Currently, I'm solving this issue by using loops, but I'm wondering if there's a more efficient way to solve that?</p>
  
  <pre><code>mat_zeros = [];
  mat_ones = [];
  
  for m=1:M
      for n=1:N
  
          matval = matrixbig(m,n,:);
          matval = matval(:)'; % mapping matval to a K-dimensional vector
  
          if (binmask(m,n) == 1)
              mat_ones   = [mat_ones; matval];
          elseif (binmask(m,n) == 0)
              mat_zeros   = [mat_zeros; matval];
          end
      end
  end
  </code></pre>
  
  <p>All suggestions are welcomed ;-)</p>