Find X for a given Y using interpolation
40 次查看(过去 30 天)
显示 更早的评论
Hi all,
Suppose
x = [5 8 9 11 12.5 17];
y = [2 3.5 4.0 5.3 6.7 8.2];
Now I want to find the corresponding value for x where y = 4.8 using interpolation. I was wondering if there is any function in MATLAB which can do this calculation.
0 个评论
采纳的回答
Dyuman Joshi
2023-9-22
x and y are not mapped one-to-one for interpolation, there is an element missing in y.
interp1 is what you are looking for.
8 个评论
Dyuman Joshi
2023-9-22
"What does "Sample points must be unique." mean?"
Let's introduce a repeating value in the data set -
y = [2 2 3.5 4.0 5.3 6.7 8.2 ];
x = [5 6.5 8 9 11 12.5 17 ];
Now if you want to interpolate for y between 2 and 3.5, say 3, which x values should be used for interpolation?
5-8 or 6.5-8? Interpolation for such a case does not make sense.
So, we have to use non-repeating values - a.k.a unique values in MATLAB terminology. That's what the error is stating.
Now, as 6.5-8 is a subset of the range 5-8 for the same value of 2, we can eliminate it from our data and then interpolate -
%Get unique values of the array in sorted manner
[y,idx] = unique(y)
x = x(idx)
interp1(y,x,3)
Note that this is just one of the possible cases of data with repeating values.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!