高高手帮忙啊!!!!!求matlab能算出一组数列第一个峰值和谷值的程序!

有一组数列 0 1 2 3 4 5 6 7 8 7 6 5 4 3 4 5 6 5.... 就是这个意思!随便写了一个!第一个峰值和谷值分别是8和3!怎么样用matlab编程求出第一个峰值和谷值!谢谢啦!

搜索原理:从第一个数据开始搜索,当发现这个数大于等于前一个数且小于等于后一个数时,就是峰值了,搜索谷值的原理也相同,即找到小于等于前一个数且大于等于后一个数的数。
a = [ 0 1 2 3 4 5 6 7 8 7 6 5 4 3 4 5 6 5];
[ROW, COLUMN] = size(a); //ROW为矩阵a的行数,COLUMN为列数
for i = 2:COLUMN //搜索峰值
if a(i)>=a(i-1) && a(i)<=a(i+1)
max = a(i);
break;
end

for i = 2:COLUMN //搜索谷值
if a(i)<=a(i-1) && a(i)>=a(i+1)
min = a(i);
break;
end
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-13
使用findpeaks即可满足你的要求!
help findpeaks
FINDPEAKS Find local peaks in data
PKS = FINDPEAKS(X) finds local peaks in the data vector X. A local peak
is defined as a data sample which is either larger than the two
neighboring samples or is equal to Inf.

[PKS,LOCS]= FINDPEAKS(X) also returns the indices LOCS at which the
peaks occur.

[...] = FINDPEAKS(X,'MINPEAKHEIGHT',MPH) finds only those peaks that
are greater than MINPEAKHEIGHT MPH. Specifying a minimum peak height
may help in reducing the processing time. MPH is a real valued scalar.
The default value of MPH is -Inf.

[...] = FINDPEAKS(X,'MINPEAKDISTANCE',MPD) finds peaks that are at
least separated by MINPEAKDISTANCE MPD. MPD is a positive integer
valued scalar. This parameter may be specified to ignore smaller peaks
that may occur in close proximity to a large local peak. For example,
if a large local peak occurs at index N, then all smaller peaks in the
range (N-MPD, N+MPD) are ignored. If not specified, MPD is assigned a
value of one.

[...] = FINDPEAKS(X,'THRESHOLD',TH)finds peaks that are at least
greater than their neighbors by the THRESHOLD TH. TH is real valued
scalar greater than or equal to zero. The default value of TH is zero.

[...] = FINDPEAKS(X,'NPEAKS',NP) specifies the maximum number of peaks
to be found. NP is an integer greater than zero. If not specified, all
peaks are returned.

[...] = FINDPEAKS(X,'SORTSTR',STR) specifies the direction of sorting
of peaks. STR can take values of 'ascend','descend' or 'none'. If not
specified, STR takes the value of 'none' and the peaks are returned in
the order of their occurrence.
相似回答