MATLAB Answers

0

Interpolating from set of vectors to another with some tolerance?

Asked by John Moran IV on 3 Jul 2019
Latest activity Commented on by Stephen Cobeldick on 3 Jul 2019
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

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.
John Moran IV's "Answer" moved here:
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.

Sign in to comment.

1 Answer

Answer by John D'Errico
on 3 Jul 2019
Edited by John D'Errico
on 3 Jul 2019
 Accepted Answer

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

x_CCX = Temp_results(:,1);
x_diff = zeros(N_Temps,1);
T_int = zeros(N_Temps,1);
for i=1:N_Temps
min_diff_j = 0;
min_diff = 100000; % Some arbitrarily large number
for j=1:N_Temps_Sierra
if abs(x_CCX(i)-x_Sierra(j)) < min_diff
min_diff = abs(x_CCX(i)-x_Sierra(j));
min_diff_j = j;
end
end
% The closest values get used for the interpolation values
x_diff(i) = abs(x_CCX(i)-x_Sierra(min_diff_j));
T_int(i) = T_Sierra(min_diff_j);
end
How can I reduce this process or do it simpler? I'm not a very advanced MATLAB user and also the x-position values for both sets are not sorted. I could rearrange them before doing my interpolation process, but would that be faster/less lines?
EDIT: It does seem like you understand what I'm going for, sorry for the confusion.

Sign in to comment.