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]

最新更新