Suppose we have an array with data:
double[] x = new double[N] {x_1, ..., x_N};
And array of size N containing labels corresponding to the elements of x:
int[] ind = new int[N] {i_1, ..., i_N};
What is the fastest way to select all elements from x that have certain label I according to ind?
For example,
x = {3, 2, 6, 2, 5}
ind = {1, 2, 1, 1, 2}
I = ind[0] = 1
Result:
y = {3, 6, 2}
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 .Where and lambdas..Thanks
EDIT:
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:
T1[] xn = new T1[N] {x_1, ..., x_N};
T2[] ind = new T2[N] {i_1, ..., i_N};
T2 I = ind[0]
Using the solution provided I get an error "Delegate 'System.Func' does not take 2 arguments":
T1[] y = xn.Where((x, idx) => ind[idx] == I).ToArray();
Thank you very much