Write a function to local all local maxima and minima?

I'm having trouble using find function to output indicies of maximas and minimas in my function
So far I have this:
function [maxima,minima] = min_max(x,y)
[pks,locs] = findpeaks(y)
[pks1,locs1] = findpeaks(-y)
end
I've tried many different ways to use the find function but nothing is working out. How can I get this to output the indicies where the local mins and maxs occur?

回答 (1 件)

Image Analyst
Image Analyst 2020 年 10 月 15 日

0 投票

Try scanning across the vector from the beginning to end and see if the element is higher than both its neighbors or lower than all its neighbors
for k = 2 : length(y) - 1
if y(k) > y(k-1) && y(k) <= y(k-1)
end
end
etc. Handle the first and last element as special cases since there is only 1 neighbor, not 2.
See if you can finish your homework on your own, since we can't just answer the question for you and you can't turn in our work as your own.

4 件のコメント

Tyler Bodnarik
Tyler Bodnarik 2020 年 10 月 15 日
I was able to get the maxima and minima I needed. However I'm not sure how to get the function to output the data as indicies.
Star Strider
Star Strider 2020 年 10 月 15 日
I’m sure that if you search through the documentation, you can find a function that does that!
Tyler Bodnarik
Tyler Bodnarik 2020 年 10 月 15 日
function [maxima,minima] = min_max(x,y)
[pks,locs] = findpeaks(y)
[pks1,locs1] = findpeaks(y*-1)
maxima = find(y == pks)
minima = find(y == pks1)
so i have the values of the local maximas and minimas with pks,locs,pks1,locs1 but I've tried getting the indicies many different ways. The function needs to output the indicies of the input vector, so that would be when the input vector's x value = loc and loc1 correct? Sorry if i explained that badly.
Star Strider
Star Strider 2020 年 10 月 15 日
There is a difference between the output islocalmin and its friends produce and the output findpeaks produces.
That difference is important.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGenomics and Next Generation Sequencing についてさらに検索

質問済み:

2020 年 10 月 15 日

編集済み:

2020 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by