How to solve nearest value, only searching on x, (not Y)?
Moderator: Arction_LasseP
How to solve nearest value, only searching on x, (not Y)?
How do I solve for the nearest X value in a series, only specifying the x value and not caring about the Y?
My scenario is I am creating several step line hi low series, and the x values are close in time range but not exactly at the same time.
Sometimes the cooresponding value is a few seconds before or after the others.
So I need a method that solves forward or backward for the closest x value.
The Y value is irrelevant to this search, and unknown at the time of the search...
Any Ideas ?
Thanks,
Heather
My scenario is I am creating several step line hi low series, and the x values are close in time range but not exactly at the same time.
Sometimes the cooresponding value is a few seconds before or after the others.
So I need a method that solves forward or backward for the closest x value.
The Y value is irrelevant to this search, and unknown at the time of the search...
Any Ideas ?
Thanks,
Heather
 ArctionPasi
 Posts: 1366
 Joined: Tue Mar 26, 2013 10:57 pm
 Location: Finland
Re: How to solve nearest value, only searching on x, (not Y)
Just iterate thru the data point array and find the nearest diff.
Like
Like
Code: Select all
double nearestDist = double.MaxValue;
int nearestIndex = 1;
for(int i=0;i<series.PointCount;i++)
{
double dist = Math.Abs(x  dataPoints[i].X);
if(dist < nearestDist)
{
nearestDist = dist;
neartestIndex =i;
}
}
LightningChart Support Team, PT
Re: How to solve nearest value, only searching on x, (not Y)
Hi Pasi,
I currently am doing that, but wondered if there was a better method...
Thanks,
Heather
I currently am doing that, but wondered if there was a better method...
Thanks,
Heather

 Posts: 393
 Joined: Mon Mar 14, 2016 9:22 am
Re: How to solve nearest value, only searching on x, (not Y)
Hi Heather,
A little bit extended answer.
SolveNearestDataPointByCoord tries to find the points with shortest Euclidean distance. If you have other metric for shortest distance than "Euclidean distance", you could implement yourself.
If your points (x coordinates) in progressive order, there are some techniques, which are more optimal than just running straightforward FOR loop.
All the best.
A little bit extended answer.
SolveNearestDataPointByCoord tries to find the points with shortest Euclidean distance. If you have other metric for shortest distance than "Euclidean distance", you could implement yourself.
If your points (x coordinates) in progressive order, there are some techniques, which are more optimal than just running straightforward FOR loop.
All the best.
Re: How to solve nearest value, only searching on x, (not Y)
Hello and thanks for the details,
Yes my data is progressive, and I really would like to optimize it better than just iterating through the data...
If you might have some additional tips / pointers handy, I would really appreciate it.
Thanks again for all your help and information!
Heather
Yes my data is progressive, and I really would like to optimize it better than just iterating through the data...
If you might have some additional tips / pointers handy, I would really appreciate it.
Thanks again for all your help and information!
Heather

 Posts: 393
 Joined: Mon Mar 14, 2016 9:22 am