title: MATLAB: extract submatrix with logical indexing
body: <p>I'm looking for an elegant solution to this very simple problem in MATLAB. Suppose I have a matrix </p>
  
  <pre><code>&gt;&gt; M = magic(5)
  
  M =
  
      17    24     1     8    15
      23     5     7    14    16
       4     6    13    20    22
      10    12    19    21     3
      11    18    25     2     9 
  </code></pre>
  
  <p>and a logical variable of the form</p>
  
  <pre><code>I =
  
       0     0     0     0     0
       0     1     1     0     0
       0     1     1     0     0
       0     0     0     0     0
       0     0     0     0     0
  </code></pre>
  
  <p>If I try to retrieve the elements of <code>M</code> associated to <code>1</code> values in <code>I</code>, I get a column vector </p>
  
  <pre><code>&gt;&gt; M(I)
  
  ans =
  
       5
       6
       7
      13
  </code></pre>
  
  <p>What would be the simplest way to obtain the matrix <code>[5 7 ; 6 13]</code> from this logical indexing? </p>
  
  <p>If I know the shape of the non-zero elements of <code>I</code>, I can use a reshape after the indexing, but that's not a general case. </p>
  
  <p>Also, I'm aware that the default behavior for this type of indexing in MATLAB enforces consistency with respect to the case in which non-zero values in <code>I</code> do not form a matrix, but I wonder if there is a simple solution for this particular case.</p>