How to find x value for a known y in array plot?
조회 수: 14 (최근 30일)
이전 댓글 표시
I have to draw a plot based on a long matrix, which I did, after that I got the maximum of y value (max(y)) but I need the x value for the known y value y=0.8*max(y) THIS y value is not included in the matrix, so I can't use the Index of the maximum to get my x
I did found a similar question https://de.mathworks.com/matlabcentral/answers/258556-how-to-i-find-a-x-value-from-a-given-y
and tried to apply the same code x(y==0.8*max(y)) but I get a 0x1 empty column vector.
Can anyone help me out? thanks in advance
댓글 수: 0
채택된 답변
Star Strider
2022년 8월 7일
Possibly:
idx = find(diff(sign(y - 0.8*max(y))));
xvals = x(idx)
That should find the closest indices to all of them.
댓글 수: 2
Star Strider
2022년 8월 10일
편집: Star Strider
2022년 8월 11일
Without the data, it is difficult to determine that. It will be necessary to define a specific x-value to determine which is closest.
Example —
x = linspace(0, 20, 500); % Create Data
y = sin(2*pi*x) + cos(3*pi*x) - (x-10).^2/100; % Create Data
idx = find(diff(sign(y - 0.8*max(y))));
xvals = x(idx)
x_desired = 10; % Define Target X-Value
[xabsdist,xidx] = min(abs(xvals - x_desired));
% idx(xidx)
% x(idx(xidx))
% y(idx(xidx))
fprintf(1,'Target Y-Value: %.3f\nClosest X-Value: %.3f\nCorresponding Y-Value: %.3f\nY-Distance |Desired-Actual|: %.3f\n', 0.8*max(y), x(idx(xidx)), y(idx(xidx)), abs(0.8*max(y)-y(idx(xidx))))
figure
plot(x, y, 'DisplayName','Data')
hold on
plot(xvals, y(idx), 'ks', 'DisplayName','Closest Indices')
plot(x(idx(xidx)), y(idx(xidx)), 'rs', 'MarkerFaceColor','r', 'DisplayName','Closest X-Value')
yline(0.8*max(y), '--g', 'DisplayName','0.8\times y_{max} Reference')
xline(x_desired, '--r', 'DisplayName','Desired X-Value')
hold off
grid
legend('Location','best')
This uses synthetic data, however it should work for your actual data.
Make appropriate changes to get the result you want. (It is also possible to interpolate to get the exact x- and y-values rather than just using the indices.)
EDIT — (11 Aug 2022 at 1:00)
Added interpolation example.
Example —
tgt = 0.8*max(y);
L = numel(x);
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(L,idx(k)+1);
xv(k) = interp1(y(idxrng), x(idxrng), tgt);
end
[xvabsdist,xvidx] = min(abs(xv - x_desired));
fprintf(1,'Closest X-Value: %.3f\nDistance |xv - x-desired|: %.3f\n', xv(xvidx),xvabsdist)
figure
plot(x, y, 'DisplayName','Data')
hold on
plot(xv, ones(size(xv))*tgt, 'ks', 'DisplayName','Exact X-Values')
plot(xv(xvidx), tgt, 'rs', 'MarkerFaceColor','r', 'DisplayName','Closest X-Value')
yline(0.8*max(y), '--g', 'DisplayName','0.8\times y_{max} Reference')
xline(x_desired, '--r', 'DisplayName','Desired X-Value')
hold off
grid
legend('Location','best')
.
추가 답변 (4개)
David Hill
2022년 8월 7일
[~,idx]=min(abs(y-.8*max(y)));%.8*max(y) is not part of the y-array, find the closest y-idx to that value
x(idx)
Bruno Luong
2022년 8월 10일
편집: Bruno Luong
2022년 8월 10일
*
x = linspace(-3,3);
Assuming your data are monotonic on the neighborhood of the max
y = exp(-x.^2);
[maxy, imax] = max(y);
yt = 0.8*maxy;
% left side
i1 = find(y<yt & 1:length(y)<imax, 1, 'last');
if isempty(i1)
error('no x found on the left side')
end
xl = interp1(y([i1 i1+1]), x([i1 i1+1]), yt)
% right side
i2 = find(y<yt & 1:length(y)>=imax, 1, 'first');
if isempty(i2)
error('no x found on the right side')
end
xr = interp1(y([i2-1 i2]), x([i2-1 i2]), yt)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

