x value on y max
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi! How do I get matlab to show me the x value on my max y value that I have in my function and in my graph. I have already tried to find it by using plot(x,y) but don't know how.
Would be really nice to get an answer on my simple question.
Sincerely Simon
채택된 답변
Image Analyst
2013년 4월 17일
편집: Image Analyst
2013년 4월 17일
4 개 추천
xIndex = find(y == max(y), 1, 'first');
maxXValue = x(xIndex);
Or
[maxYValue, indexAtMaxY] = max(y);
xValueAtMaxYValue = x(indexAtMaxY(1));
The (1) is there in case the max occurs at multiple places, it takes the first.
댓글 수: 14
Simon
2013년 4월 17일
Thank you, the second one did it. But how do I get Matlab to show it in my plot figure?
Image Analyst
2013년 4월 18일
편집: Image Analyst
2013년 4월 22일
Try
hold on;
plot(xValueAtMaxYValue, maxYValue, 'r+', 'MarkerSize', 30);
Simon
2013년 4월 20일
That was not really what I were lookin for, it shows in a figure a red cross where the max value is, but it doesn't show it on the original plot line figure.
Image Analyst
2013년 4월 20일
I don't understand what the difference is. Did you use "hold on" so as to not blow away the original plot? Maybe you could upload a picture of your desired plot(s).
Oh, I totally missed hold on; Sorry about that, but thanks for the answer, it works now.
Brett Ruben
2020년 11월 14일
This is a very old thread but helped me find the max value, so thank you! Using this as a guide, am I able to find the x value at 10% of the max value? I have a proton Bragg peak and the range of a proton in water is determined at 10% of the max value after the max of the curve. Please let me know if you can assist. I tried simply dividing the "max(y)" by 10, but that did not work. I used the second of your two suggestions above to find the max. Thank you!
Image Analyst
2020년 11월 15일
Brett, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
echo off;
% Make some sample oscillating signal.
t = 1 : 20;
signal = abs(cos(t)) .* exp(-0.1*t);
plot(t, signal, 'b.-', 'LineWidth', 2, 'MarkerSize', 15);
title('Proton Bragg peak and the range of a proton in water', 'FontSize', 15, 'Interpreter', 'none');
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
grid on;
% Find max of the entire signal value and it's location.
[maxSignal, indexAtMaxSignal] = max(signal);
fprintf('Found the first max at t = %.1f, it has a value there of %f.\n', t(indexAtMaxSignal), maxSignal);
xline(t(indexAtMaxSignal), 'LineWidth', 2, 'Color', 'r');
% Find max signal value from indexAtMaxSignal to the end, and it's location.
[maxSignal2, indexAtMaxSignal2] = max(signal(indexAtMaxSignal+1:end));
% Add in the offset so we get the index from the original signal
indexAtMaxSignal2 = indexAtMaxSignal2 + indexAtMaxSignal;
fprintf('Found the second max at t = %.1f, it has a value there of %f.\n', t(indexAtMaxSignal2), maxSignal2);
xline(t(indexAtMaxSignal2), 'LineWidth', 2, 'Color', 'r');
% Find the value that is 10% of the SECOND max.
v10Percent = signal(indexAtMaxSignal2) * 0.10
yline(v10Percent, 'LineWidth', 2, 'Color', 'g');
% Find out where that occurs
% Make copy of signal
s2 = signal;
% Erase up to indexAtMaxSignal2
s2(1 : indexAtMaxSignal2) = inf;
t10 = find(s2 < v10Percent, 1, 'first')
xline(t10, 'LineWidth', 2, 'Color', 'g');
% Shade the range
shareColor = 'g';
xl = xlim();
yl = ylim();
yFill = [yl(1), yl(1), yl(2), yl(2), yl(1)];
x1 = t(indexAtMaxSignal2);
x2 = t(t10);
xFill = [x1, x2, x2, x1, x1];
hold on;
fillHandle = fill(xFill, yFill, shareColor, 'FaceAlpha', 0.1);

The first/left red line is the overall max. The second/right red line is the max AFTER the first max. The horizontal green line is 10% of that second max. The green vertical line is the first place in the data, after the second max, where the data falls below that 10% value. The range is tinted light green. Is this what you meant?
Siddharth Singh Chauhan
2021년 1월 22일
I have two data one for x and one for y.
I need to find max(y) and the corresponding value for x.
In the most simple way.
Image Analyst
2021년 1월 23일
Siddarth:
Try this
maxY = max(y);
indexes = find(y == maxY);
% Get the x values everywhere that y is maxY
maxX = x(indexes); % May be at more than 1 x location.
Of more compactly:
maxX = x(y == max(y)); % May be at more than 1 x location.
O. T.
2021년 1월 28일
Hi,
I have also very similar problem. I have a lot of text file in one folder and each has only two column data as x and y. I want to read each text file and find an x value for maximum y, and extract it as a seperate text file. So final text file would be a single column of only x value of maximum y values for about thousands of text file. Thank you very much in advanced.
Image Analyst
2021년 1월 29일
I don't know what "extract it as a seperate text file" means? Once you've read in x and y and gotten the x at the maximum y, it's already been put into a variable. There is no text file to extract.
for k = 1 : numFiles
fileName = allFilenames{k}
xy = readmatrix(fileName)
[maxY(k), index(k)] = max(xy(:, 2));
maxX(k) = xy(index(k), 1); % Get x value where y is max.
% Now you have maxX(k). There is no way to "extract" it to a file.
% You can "write" it if you want, but not extract it.
end
O. T.
2021년 1월 29일
Hello Image Analyst
Thank you very much for the answer. I have this error when I run this script. What can be the problem? Yes, What I want is to write it in seperate text file, exactly.
Undefined function or variable 'numFiles'.
Error in value_x_in_max_y_3 (line 2)
for k = 1 : numFiles
khaled elbatawy
2022년 10월 14일
Hello,
@Image Analyst i know that it is old answer but it is related somehow to my Problem.
The different is my Data (y) is oscilating.
I want for every Maxima/Minima of y to find the corresponding Value of x. But without using findpeaks(Data)because it is needed the Signal Processing Toolbox.
Thank you in advance !
Image Analyst
2022년 10월 14일
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
