title: Selecting elements from array according to indices specified in another array c#
body: <p>Suppose we have an array with data:</p>
  
  <pre><code>double[] x = new double[N] {x_1, ..., x_N};
  </code></pre>
  
  <p>And array of size <code>N</code> containing labels corresponding to the elements of <code>x</code>:</p>
  
  <pre><code>int[] ind = new int[N] {i_1, ..., i_N};
  </code></pre>
  
  <p>What is the fastest way to select all elements from <code>x</code> that have certain label <code>I</code> according to <code>ind</code>?</p>
  
  <p>For example,</p>
  
  <pre><code>x = {3, 2, 6, 2, 5}
  ind = {1, 2, 1, 1, 2}
  I = ind[0] = 1
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>y = {3, 6, 2}
  </code></pre>
  
  <p>Clearly, it can be easily (but not efficiently and clean) done with loops, but I think there should be the way how to do that using <code>.Where</code> and lambdas..Thanks</p>
  
  <p>EDIT:</p>
  
  <p>The answer provided by MarcinJuraszek is completely correct, thanks. However, I've simplified the question in hope that it would work in my original situation. Could you please take a look what is the problem if we have generic types:</p>
  
  <pre><code>T1[] xn = new T1[N] {x_1, ..., x_N};
  T2[] ind = new T2[N] {i_1, ..., i_N};
  T2 I = ind[0]
  </code></pre>
  
  <p>Using the solution provided I get an error "Delegate 'System.Func' does not take 2 arguments":</p>
  
  <pre><code>T1[] y = xn.Where((x, idx) =&gt; ind[idx] == I).ToArray();
  </code></pre>
  
  <p>Thank you very much</p>