Matlab 中的局部最大值

Local Maximas in Matlab

在Matlab中,通过函数max(),我只能得到一个向量的单个最大元素,即使可以有多个局部最大元素。我想知道如何在 MATLAB 中获取所有局部最大值的值。

局部最大值将始终具有此特征:

1)先验点总是少的。 2)后面的点永远少

所以你可以这样做:

% Example input vector a
a = [0 1 2 1 2 3 2 1 0 1 2 1 5 2 1 4 1];

% Shift vectors so that prior point and next point lines up with
% current point. 
prior_points   = a(1:(length(a) - 2));
current_points = a(2:(length(a) - 1));
next_points    = a(3:length(a));

% Identify indices where slope is increasing from prior to current
% and decreasing from current to next.
prior_slope_positive = ((current_points  - prior_points)    > 0);
next_slope_negative  = ((next_points     - current_points)  <= 0);

% This will return indices of local maximas.  Note that the result
% is incremented by one since current_points was shifted once.
b = find(prior_slope_positive & next_slope_negative) + 1;

请注意,此示例不包括第一个和最后一个点作为潜在的局部最大值。

向量a中的局部最大值是: 指标值 3 2 6 3 11 2 13 5 16 4

因此,最终向量 b 将等于: [3 6 11 13 16]