Page 1 of 1

How to solve nearest value, only searching on x, (not Y)?

Posted: Thu Sep 21, 2017 12:15 pm
by plotter
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

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Thu Sep 21, 2017 1:37 pm
by ArctionPasi
Just iterate thru the data point array and find the nearest diff.

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; 
}
}

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Thu Sep 21, 2017 3:49 pm
by plotter
Hi Pasi,
I currently am doing that, but wondered if there was a better method...
Thanks,
Heather

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Mon Sep 25, 2017 9:45 am
by ArctionKestutis
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.

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Tue Sep 26, 2017 8:06 pm
by plotter
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

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Wed Sep 27, 2017 11:09 am
by ArctionKestutis

Re: How to solve nearest value, only searching on x, (not Y)

Posted: Thu Sep 28, 2017 5:27 pm
by plotter
Thank you!