Interpolating from set of vectors to another with some tolerance?

조회 수: 5 (최근 30일)
John Moran IV
John Moran IV 2019년 7월 3일
댓글: Stephen23 2019년 7월 3일
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
Guillaume
Guillaume 2019년 7월 3일
편집: Guillaume 2019년 7월 3일
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
Stephen23 2019년 7월 3일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2019년 7월 3일
편집: John D'Errico 2019년 7월 3일
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
John Moran IV
John Moran IV 2019년 7월 3일
편집: John Moran IV 2019년 7월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by