How do I determine if the desired value in a vector is the Max or Min of the values around it
1 回表示 (過去 30 日間)
古いコメントを表示
I have the Vector x that I want to find the Max or Min such as:
x = [ 21 19 20 17 16 17 18 16 15 13 15 16]
min(concave up) = [19 16 13]
Max(concave down) = [20 18 ]
This is what I have been trying.
I do not know if their is a fuction already in MatLab, but I would like to do it using a script.
Thank you in advance.
y = [1 2 3 4 5 6 7 8 9 10 11 12];
x = [21 19 20 17 16 17 18 16 15 13 15 16];
s = 1;
% this to get the Min
for i = 2:size(x,2)-1
if x(i-1)< x(i) < x(i+1)
z(S)= x(i);
S = S + 1;
end
end
0 件のコメント
採用された回答
Adam Danz
2019 年 11 月 24 日
編集済み: Adam Danz
2019 年 11 月 24 日
Differentiate x and determine which values are falling (negative) or rising (positive). Local minima are where the pattern switches from negative to positive. Local maxima are where the pattern switchs from positive to negative. The first and last samples are not considered minima or maxima.
In the code below, localMinIdx and localMaxIdx are the index values of (x) locating the local mins and max's.
localMins and localMaxs are the local mins and max's.
x = [ 21 19 20 17 16 17 18 16 15 13 15 16];
localMinIdx = strfind([0,diff(x),1]<0,[1,0]);
localMins = x(localMinIdx); % = [19 16 13]
localMaxIdx = strfind([0,diff(x),1]>0,[1,0]);
localMaxs = x(localMaxIdx); % = [ 20 18]
0 件のコメント
その他の回答 (1 件)
Andrei Bobrov
2019 年 11 月 25 日
x = [ 21 19 20 17 16 17 18 16 15 13 15 16];
M = x(hankel(1:3,3:numel(x)));
% max
[peakmax,i] = max(M);
peakmax = peakmax(i == 2);
% min
[peakmin,i] = min(M);
peakmin = peakmin(i == 2);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Preprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!