Clear Filters
Clear Filters

Find peaks in a for loop?

2 views (last 30 days)
Troth
Troth on 26 Oct 2022
Answered: Star Strider on 26 Oct 2022
i have a 1007x94 matrix, how would I write a for loop to find the peak in every row?

Accepted Answer

Star Strider
Star Strider on 26 Oct 2022
The maxk and mink functions (introduced in R2017b) would make this a bit more efficient. Lacking them, use sort.
Try this —
A = randn(1007, 94);
for k = 1:size(A,2)
[pks, locs] = findpeaks(A(:,k));
[~,ix] = sort(pks);
minpk{:,k} = [pks(ix(1:2)) locs(ix(1:2))];
maxpk{:,k} = [pks(ix(end-1:end)) locs(ix(end-1:end))];
end
maxpk{1} % View Result
ans = 2×2
3.0201 816.0000 3.3828 355.0000
minpk{1} % View Result
ans = 2×2
-1.0253 384.0000 -0.9567 702.0000
.

More Answers (1)

Vasudev Sridhar
Vasudev Sridhar on 26 Oct 2022
Assuming you mean maximum when you say peak.
a = randi(20, 2,10)
a = 2×10
15 18 17 14 2 6 17 1 7 9 6 17 18 18 4 5 17 19 1 18
result = max(a,[],2)
result = 2×1
18 19
  1 Comment
Troth
Troth on 26 Oct 2022
My goal is to write a loop to find the 2 highest peaks and two lowest of each column (not row I made a mistake) and each index which it occurs, but to use the find peaks function to do so.

Sign in to comment.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!