在Matlab中,通过函数max(),我只能获得一个向量的单个最大元素,即使可以存在多个局部最大元素。我想知道如何在MATLAB中获取所有局部最大值的值。
答案 0 :(得分:2)
本地最大值将始终具有此特征:
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]