title: Implementing the matlab find function
body: <p>I am trying to find the coordinates of all non zero elements in a matrix, in MATLAB. I know that <code>find</code> makes this very easy. The problem is that I need to define the length of my matrix before I can fill it up. I believe this is impossible using <code>find</code>, since it will make its own vector.</p>
  
  <p>So I have to find another way. If I'm looking for 1 element it will be easy as well. Let me use an example. Say I have a matrix <code>M</code> of 500×500, with <code>60</code> ones somewhere. I want to find the coordinates. So I can start with:</p>
  
  <pre><code>for i = 1:500
    for j = 1:500
      if M(i,j) == 1
         row = i;
         col = j;
      end
    end
  end
  </code></pre>
  
  <p>But I want more then one point, and I need to define the length of a vector before filling it. So I'll make the gamble that there are less then 100 ones in the matrix:</p>
  
  <pre><code>v = zeros(1,100)
  w = zeros(1,100)
  </code></pre>
  
  <p>And how I essentially want to fill this vector is as follows. Let's say I have already filled <code>N</code> elements of the vector, so the next one will be:</p>
  
  <pre><code>for i = 1:500
    for j = 1:500
      if M(i,j) == 1 &amp;&amp; i ~= v(1) &amp;&amp; i ~= v(2) &amp;&amp; ... &amp;&amp; i ~= v(N) &amp;&amp; j ~= v(1) &amp;&amp; ... &amp;&amp; i ~= v(N)
       v(N+1) = i
       w(N+1) = j
      end
    end
  end
  </code></pre>
  
  <p>So I need another for loop containing all this, which will run through the elements of the vectors <code>v</code> and <code>w</code>, and I need some loop that will make sure the right if statement is being used.</p>
  
  <p>Does anyone have any idea of how to do this?</p>
  
  <p>Kind regards,</p>
  
  <p>Jesse</p>