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
Hi, I want to find the peak position of a XY data
3 views (last 30 days)
Show older comments
Suraj Singh
on 19 Aug 2016
Commented: Star Strider
on 20 Aug 2016
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
Accepted Answer
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
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’.
More Answers (1)
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
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)?
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!