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
2 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Star Strider
2016-8-19
编辑:Star Strider
2016-8-19
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 个评论
Star Strider
2016-8-20
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’.
更多回答(1 个)
Azzi Abdelmalek
2016-8-19
编辑:Azzi Abdelmalek
2016-8-19
x=(0:9)'
y=rand(10,25) % 10x25 array
max_val=max(y,[],2)
plot(x,max_val)
3 个评论
Azzi Abdelmalek
2016-8-20
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)?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!