title: Storing non-zero integers from one matrix into another
body: <p>I'm attempting to create a loop that reads through a matrix (A) and stores the non-zero values into a new matrix (w). I'm not sure what is wrong with my code.</p>
  
  <pre><code>function [d,w] = matrix_check(A)
  [nrow ncol] = size(A);
  total = 0;
  for i = 1:nrow
      for j = 1:ncol
          if A(i,j) ~= 0
              total = total + 1;
          end
      end
  end
  d = total;
  w = [];
  for i = 1:nrow
      for j = 1:ncol
          if A(i,j) ~= 0
              w = [A(i,j);w];
          end
      end
  end
  </code></pre>
  
  <p>The second loop is not working (at at least it is not printing out the results of w).</p>