Clear Filters
Clear Filters

Hi, I want to find the peak position of a XY data

3 views (last 30 days)
I looked on some earlier posts but my problem didn't matches with other problems exactly, Actually I have a single column data of X but Y data contains 25 columns. Each column of Y data is taken at the same X column data, I want to find Ymax along with Its X value for each column of Y data, and try to plot Ymax with its X values
  1 Comment
Suraj Singh
Suraj Singh on 19 Aug 2016
I have tried [peakValues, indexes] = findpeaks(y); but it is giving wrong index values (x),and at some column of y data its giving more than one peak and index values

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 19 Aug 2016
Edited: Star Strider on 19 Aug 2016
If you only have one maximum in each column (no repeats), this works:
x = linspace(0, 1, 30)'; % Create ‘x’ Vector
Y = rand(30, 25)*90; % Create ‘Y’ Matrix
[ymax,idx] = max(Y); % First (Or Only) Value Of Each Column, & Index
figure(1)
plot(x(idx), ymax, 'bp') % Plot ‘ymax’ With Corresponding ‘x’ Value
grid
It plots the first or only maximum ‘y’ value in each column with its corresponding ‘x’ value.
EDIT —
If you have more than one value of the maximum in any one column, it is necessary to use a different strategy:
[rowidx,colidx] = find(bsxfun(@minus, Y, max(Y)) == 0); % Row & Column Indices For All Maxima
linidx = sub2ind(size(Y),rowidx,colidx);
figure(2)
plot(x(rowidx), Y(linidx), 'bp') % Plot ‘ymax’ With Corresponding ‘x’ Value
grid
  2 Comments
Suraj Singh
Suraj Singh on 20 Aug 2016
Thanks man ! Its working. In case of mine the data can't have more than one maxima, but may be more than one peak, and i have to choose the max of that peak. Then how it could be easy to plot?
Star Strider
Star Strider on 20 Aug 2016
My pleasure!
My code plots the maximum for each column against the ‘x’ value for that value in figure(1). If you want to plot them as a line instead of points, you have to sort them by the index variable ‘idx’ first, otherwise the line is just a jumble.
The code changes to:
[ymax,idx] = max(Y); % First (Or Only) Value Of Each Column, & Index
[sidx,srtidx] = sort(idx); % Sort ‘idx’ Ascending —> ‘sidx’ For ‘x’ & ‘srtidx’ For ‘ymax’
figure(1)
plot(x(sidx), ymax(srtidx))
grid
The ‘sidx’ variable are the sorted values of ‘idx’, and ‘srtidx’ are the indices of the original positions of the ‘srtidx’ values. See the documentation for sort for details.
That should do what you want. The first code works also, it just plots points rather than a line. It is possible that the different column maxima occur at the same value of ‘x’, so there would be two blue stars or a vertical line at that ‘x’.

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Aug 2016
Edited: Azzi Abdelmalek on 19 Aug 2016
x=(0:9)'
y=rand(10,25) % 10x25 array
max_val=max(y,[],2)
plot(x,max_val)
  3 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 20 Aug 2016
You can calculate the max of y for each column
x=(0:9)'
y=rand(10,25) % 10x25 array
max_val=max(y)
but now, x length is 10, and max_val length is 25. How will you plot (x,mx_val)?
Suraj Singh
Suraj Singh on 20 Aug 2016
Your are right, but In mine case the the elements in X column and each column of Y are same. And i don,t have to plot ymax with x data but ymax with the corresponding x values at which peak appears

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!