title: Find the first non-zero column of a matrix (vectorized edition)
body: <p>I am asking myself what the most "vectorized" solution for finding the values of the first non-zero column of a matrix would look like. If a vectorized solution exists but is very ugly/hackish, I am also asking for the most elegant solution.</p>
  
  <p>Let's say we have a Matrix <code>M</code>, for which we can assume that it contains at least one non-zero value:</p>
  
  <pre><code> M =
       0     0     1     0     0
       0     0     2     0     0
       0     0     3     0     42
       0     0     4     0     0
       0     0     0     0     0
  </code></pre>
  
  <p>and I would like to find the values in the first column which is not all zeros; so for this example the desired output would be:</p>
  
  <pre><code> column = 
      1
      2
      3
      4
      0
  </code></pre>
  
  <p>My first attempt employed a <code>for</code> loop and is working alright, but it is probably not making full use of the tools available in matlab.</p>
  
  <pre><code>&gt;&gt; for i = 1:size(M,2)
      col = M(:,i);
      if find(col) % empty array evaluates to false
          break;
      end
  end
  &gt;&gt; col
  col =
       1
       2
       3
       4
       0
  </code></pre>
  
  <p>Another solution I came up with makes use of nested <code>find</code> and <code>cellfun</code> calls, but it's probably still not the best approach to the problem.</p>
  
  <pre><code>&gt;&gt; C = find(cellfun(@isempty, cellfun(@find, num2cell(M,1), 'UniformOutput', 0)) == 0)
  C =
       3     5
  &gt;&gt; M(:,C(1))
  ans =
       1
       2
       3
       4
       0
  </code></pre>