Interpolating from set of vectors to another with some tolerance?
6 views (last 30 days)
Show older comments
Hello all,
I have two sets of vectors, each representing position and temperature data along the same length (x=0 to x=1.5). One set has 3 times the points as the other. The goal is to interpolate the difference between the sets by finding the closest value of the finer set to the course set values and then calulating the temperature difference for that pair of close values.
I was thinking I could iterate through the sets used nested for loops and a conditional which used a tolerance to determine if two x positions from the sets were close enough to be used for the difference calculation.
EDIT: I think the nested loop strategy could work except that ultimately I want the closest point I can get, not necessarily the first one that is "within tolerance".
Is there a better way to do this?
Thank you for your time,
Best,
John
2 Comments
Guillaume
on 3 Jul 2019
Edited: Guillaume
on 3 Jul 2019
Whatever it is you're trying to do, I'm confident that it can be done more simply and efficiently without any loop.
However I'm confused, if you interpolate the temperatures of the coarse position using the fine positions, then the positions of the 2 sets (interpolated and fine) will exactly match. That's the whole point of interpolation.
Stephen23
on 3 Jul 2019
I've included the script I'm trying to run which might give you an idea of what I'm trying to do. I want to keep the x positions of the course set to stay and then I want to interpolate the closest positions of the fine set based on position distance. The idea is to calculate error norms but I need matching x positions for the two sets to do this first.
Accepted Answer
John D'Errico
on 3 Jul 2019
Edited: John D'Errico
on 3 Jul 2019
Confusing as hell.
You have one set of values as a pair of vectors, (xfine_i,Tfine_i). As well, you have the same thing at a coarse set of points, so (xcoarse_j,Tcoarse_j).
Now, you want to find the closest point for each coarse point xcoarse_j, in the fine set? A double set of loops is wild overkill to do that. If x_i is sorted, then just use discretize. It will identify which pair of values xfine_i and xfine_(i+1) that point lies between, and it is VECTORIZED!!!!!
Once you have this correspondence, just pick the closer of the two. If the distance is too large, only you know what you intend to do, but since xfine_i is supposed to be sampled 3 times as finely as the coarse set, why will that be a problem?
The point is, all of this can be done in maybe 2 or 3 lines of code, depending on what you want to do. There is no need to use doubly nested loops and a tolerance.
1 Comment
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!