title: How to efficiently manuplate very long vector?
body: <p>I have a very long vector as below in Eigen:</p>
  
  <pre><code>MatrixXi iIndex(1000000);
  </code></pre>
  
  <p>which is initialized into zeros, with only a short continuous part(less than 100) filled with <code>1</code> 's, the position of which is randomized.</p>
  
  <p>I need to do the following things in a long loop:</p>
  
  <pre><code>int count;
  int position;
  for(int i=0;i&lt;99999;i++){
    //... randomly fill iIndex will short `1` s
    // e.g. index = (someVectorXi.array() == i).cast&lt;int&gt;();
    count = iIndex.sum(); // count all the nonzero elements
    //position the first nonzero element index:
    for (int j=0; j&lt;1000000;j++){
      if(iIndex(j))
      position = j;
    }
  }
  </code></pre>
  
  <p>But it is really very slow.</p>
  
  <p>Is there any way to expedite?</p>