Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm still new to vb.net and have come up against another problem that should be easy to solve but after much time I still can't workout how to do it right.

I have a large 1d array of points which have unique Y's and varying X's and I want to linearly interpolate an X when provided with a Y.

I have got it working by finding the nearest point via an iterating counter through the array. Unfortunately it is too slow. I did see that .NET has the array.find with predicate but I couldn't see a sensible way of passing the known Y to the predicate?

Any suggestions gratefully received !

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 24-Mar-12 18:35pm    
You did not property describe your data. Is it sorted by X or not? (Or by Y.) Are X points placed uniformly (this is not redundant question, it's often the case in many cases of data acquisition)? What's the problem with predicate? What array function did you try?
--SA
codetowns 25-Mar-12 6:34am    
Apologies and thanks for the reply.

It is sorted low to high in Y but is not uniform. X has a range of around + 50 to - 50. So it is something like:

15,1
25,3
-15,5
-17,6
5,9
10,21
etc.

I tried the array.find but it appeared to me that the predicate function only took a point (and not the required Y without a public variable), and also would fail on a missing Y. It sounds stupid I know but I guess this is my problem of being self taught! Perhaps there is a suitable array method?


If you use a List(Of Point) instead of an array, you can use

VB
Points.Where(Function(p) p.Y = 34).First.X


Alternatively, if your Y is not excepted to be in the array, look up how to calculate the equation of a line ( y = mx + c ) and use this against a couple of points from your array, then you can use this to calculate an X for any value of Y
 
Share this answer
 
v2
Comments
codetowns 26-Mar-12 8:40am    
I was unaware of that 'points.where' function - interesting. As the straight line method was needed anyway (as you said y = mx + c)I decided to do the binary search method in the end. Thanks for the reply!
Since your array is sorted you may implement a binary search[^].
 
Share this answer
 
Comments
codetowns 26-Mar-12 8:42am    
Yes, many thanks for that link!

I've now got it searching in halves until it finds a point. Failing that it knows the two nearest ones for interpolation.
CPallini 26-Mar-12 8:48am    
You are welcome.
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900