Find peaks in a for loop?
조회 수: 2 (최근 30일)
이전 댓글 표시
i have a 1007x94 matrix, how would I write a for loop to find the peak in every row?
댓글 수: 0
채택된 답변
Star Strider
2022년 10월 26일
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
minpk{1} % View Result
.
댓글 수: 0
추가 답변 (1개)
Vasudev Sridhar
2022년 10월 26일
Assuming you mean maximum when you say peak.
a = randi(20, 2,10)
result = max(a,[],2)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!