John
John

Reputation: 447

Returning list that satisfy conditions

I have two lists filled with (x, y) data points; one of the lists being the main list and the other being a list I want to compare against.

So for example, in the main list I try to find "gaps" in the data; say I'm looking at two x data points x1 = 21 and x2 = 24. The difference between those two points is greater than deltaX = 1. So what I'd like to do, is look through the second list, and find all data points between x1 = 21 and x2 = 24 to "fill in the gap".

I have something like this:

double diffX = Math.Abs(datapoints1[i + 1].X - datapoints1[i].X);

if (diffX > deltaX)
{
    datapoints1.AddRange(LookBetween(datapoints1[i + 1].X, datapoints1[i].X);
}
.
.
.
private IEnumerable<PointXY> LookBetween(double upperLimit, double lowerLimit)
{
    return datapoints2.Where(x => x.X < upperLimit && x.X > lowerLimit);
}

LookBetween seems to return a boolean because nothing gets added to the main list. How can I get it to return a list of the x, y values that match the criteria based on the gap of x?

Upvotes: 0

Views: 98

Answers (1)

dahlbyk
dahlbyk

Reputation: 77500

It seems like you're trying to do a pairwise iteration through neighbors in datapoints1.

Using the function linked above, you might have something like:

var pairs = datapoints1.Pairwise((d1, d2) => new
{
    x1 = d1.X,
    x2 = d2.X, 
    diffX = Math.Abs(d1.X - d2.X),
});
var gaps = from pair in pairs
           where pair.diffX > deltaX
           from dp2 in datapoints2
           where pair.x1 < dp2.X && dp2.X < pair.x2
           select dp2;

Or using your LookBetween function...

var gaps = from pair in pairs
           where pair.diffX > deltaX
           from dp2 in LookBetween(pair.x2, pair.x1)
           select dp2;

Upvotes: 1

Related Questions