How do I find peaks in a matrix and extract those values?

8 views (last 30 days)
I have a matrix that is 3 columns wide. How do I find all the peaks in column 2 and pull them out with their associated values in columns 1 and 3? I would like to save these values in a new matrix.
Edit: which column I want, I didn't desribe the correct situation the first time.
Also Included image of what happens when I try to used findpeaks()
Screen Shot 2019-07-03 at 5.43.42 PM.png

Accepted Answer

Star Strider
Star Strider on 3 Jul 2019
I would do something like this:
YourMatrix = rand(20, 3); % Create Matrix
[pks,locs] = findpeaks(YourMatrix(:,1)); % Column #1 Peaks & Indices
for k = 1:2
PkVals(:,k) = YourMatrix(locs, k+1); % Corresponding Values For Columns #2 & #3
end
Experiment to get the results you want.
  5 Comments
Star Strider
Star Strider on 8 Nov 2021
This should actually be a new Question.
That aside:
time = (0:0.5:10).';
B = 1E4*exp(-(time-5.5).^2);
T = table(time,B)
T = 21×2 table
time B ____ __________ 0 7.2877e-10 0.5 1.3888e-07 1 1.6052e-05 1.5 0.0011254 2 0.047851 2.5 1.2341 3 19.305 3.5 183.16 4 1054 4.5 3678.8 5 7788 5.5 10000 6 7788 6.5 3678.8 7 1054 7.5 183.16
[pks,locs] = findpeaks(T.B, 'MinPeakHeight',6800);
times = T.time(locs,:)
times = 5.5000
A Vote would be appreciated!
.
Gabriel G
Gabriel G on 8 Nov 2021
Thanks, it works well for any other column in the table now.
I'll keep in mind for the next time to create a new question.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Jul 2019
Have you tried findpeaks() in the Signal Processing Toolbox?
How exactly do you define a peak? It's not so clear cut as you might think.
You forgot to attach your data or any screenshots. Make it easy for people to help you and you'll get more useful answers.
  2 Comments
Zach Rohde
Zach Rohde on 3 Jul 2019
I edited my question to clairfy better and inlcuded an image. Sorry about that.
Image Analyst
Image Analyst on 3 Jul 2019
Yes but no data is attached yet, and there's not even a screenshot of your signal plotted.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!