Find X-value given Y

44 次查看(过去 30 天)
Hello
I've been doing some research regarding this but I can't seem to find a solution for when I want to find the X-value given Y.
I have the following data:
>> X
ans =
20 50 100 200 500 1000 2000 5000 10000 20000
>> Y
ans =
-0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000
Where the X is plotted on a logarithmic scale. I tried interp1 with X and Y switched but that dosent work since its not strictly monotonic increasing.
Does anyone have a solution on this? Say I want to find the value of X given Y=-5

采纳的回答

Star Strider
Star Strider 2015-9-24
The ‘x’ and ‘y’ vectors were not the same sizes, so I added 25 to ‘x’, then added a small amount to the duplicated element in ‘y’ and did the interpolation:
x = [25 50 100 200 500 1000 2000 5000 10000 20000];
y = [ -0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000];
dy = diff([0 y]);
dyix = find(dy == 0);
y(dyix) = y(dyix-1)+1E-8;
xint = interp1(y, x, -10); % Find The Value Of ‘x’ Corresponding To y=-10’
  5 个评论
Star Strider
Star Strider 2015-9-25
My pleasure.
Exactly. The interp1 function has no idea how to interpret two values of the dependent variable for one value of the independent variable. (Nor would I, without making some assumptions that interp1 wisely avoids.) By clearing up that ambiguity (so that the independent variable is monotonically increasing and the duplication in it no longer exists), interp1 proceeeds. (It also needs its first two arguments to have the same size in one dimension.)
the cyclist
the cyclist 2015-9-25
Note that his X and Y vectors did actually have equal numbers of elements. They scroll off the right-hand side of the window, though.

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2015-9-24
Here is a terribly kludgy way to do it, but it gets the job done. It sorts Y, and then in the case of equal entries in Y, it will add a tiny offset. (This will work even if there are long series of consecutive equal entries.)
X = [20 50 100 200 500 1000 2000 5000 10000 20000];
Y = [-0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000];
[sortedY sortingIndex] = sort(Y);
sortedX = X(sortingIndex);
for ny = 2:numel(sortedY);
if sortedY(ny)==sortedY(ny-1)
sortedY(ny) = sortedY(ny) + eps(sortedY(ny));
end
end
interpolated_X = interp1(sortedY,sortedX,-5)
  1 个评论
Sean Eruppakkattu
Nice solution for preventing consecutive equal entries, though maybe I would use for the sake of intuitivity the following code to order the X and Y arrays:
A = [X,Y]; % matrix
res = sortrows(A,2); % matrix is ordered with respect with the Y-column
with also significant one-order speed improvement. Then the for-loop would simple refer to the column vector of the matrix.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by