Find X-value given Y
显示 更早的评论
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
采纳的回答
更多回答(1 个)
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
2019-6-1
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!