Azzera filtri
Azzera filtri

Find peaks in a for loop?

2 visualizzazioni (ultimi 30 giorni)
Troth
Troth il 26 Ott 2022
Risposto: Star Strider il 26 Ott 2022
i have a 1007x94 matrix, how would I write a for loop to find the peak in every row?

Risposta accettata

Star Strider
Star Strider il 26 Ott 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
.

Più risposte (1)

Vasudev Sridhar
Vasudev Sridhar il 26 Ott 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 Commento
Troth
Troth il 26 Ott 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.

Accedi per commentare.

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by