필터 지우기
필터 지우기

Find X-value given Y

조회 수: 29 (최근 30일)
Adaptine
Adaptine 2015년 9월 24일
댓글: Sean Eruppakkattu 2019년 6월 1일
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
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by