这就是你完成循环的方式
j = 0;
M = [];
for i = 2:size(dwcL01,1)-1
if dwcL01(i,2) > dwcL01(i-1,2) && dwcL01(i,2) > dwcL01(i+1,2)
j = j+1;
M(j, :) = dwcL01(i, :);
end
end
但是你可以通过矢量化更有效地做到这一点
%//Some example data
x = -4*pi:0.5:4*pi;
y = cos(x);
dwcL01 = [x(:), y(:)]; %// (:) just makes it a column
%// Finding the peaks using diff and sign. Note that I add the first element to the beginning as diff reduces the size by one so this prevents offsetting
F = diff(sign(diff([dwcL01(1,2);dwcL01(:,2)]))) < 0;
M = [dwcL01(F,:)', dwcL01(F,:)'];
plot(x, y, M(:,1), M(:,2), '*r')
这是如何工作的首先我们找到每个元素连续元素对的差异。现在,当符号发生变化时,这意味着我们已经达到了最大值或最小值。如果符号变化为负,则梯度从正变为负,即最大值。因此,我使用diff(sign())
查找符号更改的点,然后> 0
创建一个逻辑矩阵,其最大值为 false。然后我使用逻辑索引来提取最大值。