I’m working on an application that uses a lot of X,Y spatial information.
Given two parallel arrays, one representing the X values and the other the Y values, is there a way to select a subset from both arrays? The subset is determined by using a starting and stopping value in the x-values array.
Here’s an example of two parallel arrays.
double[] xs = new double[] {
0.0, 0.04, 0.08, 0.12, 0.16,
0.2, 0.24, 0.28, 0.32, 0.36,
0.4, 0.44, 0.48, 0.52, 0.56,
0.6, 0.64, 0.68, 0.72, 0.76,
0.8, 0.84, 0.88, 0.92
};
double[] ys = new double[] {
0.0152, 0.1434, 0.1647, 0.3800,
0.7880, 0.0488, 1.0060, 0.2710,
0.0363, 0.4321, 0.5573, 0.2192,
0.6341, 0.5181, 0.9546, 0.8275,
0.5567, 0.9870, 0.8895, 0.3423,
0.6061, 0.1507, 0.7841, 0.6512
};
Suppose I wanted a subset of both x values and y values where x is greater than or equal to 0.1 and less than or equal to 0.2. The result of such a subset should be two parallel arrays:
double[] x_subset = new double[] {
0.12, 0.16, 0.2
};
double[] y_subset = new double[] {
0.3800, 0.7880, 0.0488
};
The x and y arrays will always have the same number of elements.
I’ve tried using Zip but that doesn’t work. Apart from using plain old loops, I can’t think of anything else. I am new to LINQ, though.
May be something like this :
By using available in 4.0 version available Enumerable.Zip.