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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
