Find X-value given Y
Show older comments
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
Accepted Answer
More Answers (1)
the cyclist
on 24 Sep 2015
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 Comment
Sean Eruppakkattu
on 1 Jun 2019
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.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!