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

 Réponse acceptée

Image Analyst
Image Analyst le 17 Avr 2013
Modifié(e) : Image Analyst le 17 Avr 2013

4 votes

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 commentaires

Simon
Simon le 17 Avr 2013
Thank you, the second one did it. But how do I get Matlab to show it in my plot figure?
Image Analyst
Image Analyst le 18 Avr 2013
Modifié(e) : Image Analyst le 22 Avr 2013
Try
hold on;
plot(xValueAtMaxYValue, maxYValue, 'r+', 'MarkerSize', 30);
Simon
Simon le 20 Avr 2013
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
Image Analyst le 20 Avr 2013
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).
Simon
Simon le 22 Avr 2013
Modifié(e) : Simon le 22 Avr 2013
Oh, I totally missed hold on; Sorry about that, but thanks for the answer, it works now.
Brett Ruben
Brett Ruben le 14 Nov 2020
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
Image Analyst le 15 Nov 2020
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
Siddharth Singh Chauhan le 22 Jan 2021
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
Image Analyst le 23 Jan 2021
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.
O. T. le 28 Jan 2021
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
Image Analyst le 29 Jan 2021
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.
O. T. le 29 Jan 2021
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
khaled elbatawy le 14 Oct 2022
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 !

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by