When Y = 0.5, what is X

조회 수: 4 (최근 30일)
Ivana Smith
Ivana Smith 2022년 4월 16일
답변: Image Analyst 2022년 4월 16일
Hello!
I have a scatter plot but I want to find a way for MatLab to give me the value of X when Y=0.5 in the command window, how would I do this?

채택된 답변

Image Analyst
Image Analyst 2022년 4월 16일
You didn't attach your data, probably because you haven't yet read this:
but you can find the index where Y is that value, then use that index in X to get the X value.
index = find(Y == 0.5)
output = X(index);
If y is not exactly that (a power of 2) then you'll have to use min():
% Get sample data.
X = sort(rand(1, 100));
Y = rand(1, 100);
% Plot it.
plot(X, Y, 'b.', 'MarkerSize', 30);
grid on;
yline(0.5, 'LineWidth', 2)
% Find out which point is closest to Y = 0.5.
differenceValues = abs(Y - 0.5);
[~, index] = min(differenceValues)
outputX = X(index);
outputY = Y(index);
% Plot it.
hold on;
plot(outputX, outputY, 'ro', 'MarkerSize', 30, 'LineWidth', 2)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by