Find local maximum value in the vector

后端 未结 4 470
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 05:55

Somebody could help me. I use Matlab program.

Suppose, I have vector A,

A = [0 0 1 2 3 5 0 0 0 0 0 2 3 6 7 0 0 0 0 1 1 2 3 4 1]

I

4条回答
  •  时光取名叫无心
    2020-12-07 06:12

    I assume you are seeking local maximum values - that is, values that are greater than those around them.

    My solution would be this:

    Loc = find(diff(A)(2:end)<0 & diff(A)(1:(end-1))>0)+1;
    Val = A(Loc);
    

    Loc will contain the positions of the local maxima, and Val will contain the values at those local maxima. Note that it will NOT find maxima at the edges, as written. If you want to detect those as well, you must modify it slightly:

    Loc = find([A(1)>A(2),(diff(A)(2:end)<0 & diff(A)(1:(end-1))>0),A(end)>A(end-1)]);
    Val = A(Loc);
    

提交回复
热议问题