


localMin: Find local minima of a vector (a simplified, vectorized version)
Usage: index = localMin(x, plotOpt)
index: A 0-1 index vector indicating where the local minima are.
x: Input COLUMN vector, or a matrix
plotOpt: 1 for plotting


0001 function index = localMin(x, plotOpt) 0002 %localMin: Find local minima of a vector (a simplified, vectorized version) 0003 % Usage: index = localMin(x, plotOpt) 0004 % index: A 0-1 index vector indicating where the local minima are. 0005 % x: Input COLUMN vector, or a matrix 0006 % plotOpt: 1 for plotting 0007 0008 % Roger Jang, 19990101 0009 0010 if nargin<1, selfdemo; return; end 0011 if nargin<2, plotOpt=0; end 0012 0013 b1 = x(2:end-1, :)<=x(1:end-2, :); 0014 b2 = x(2:end-1, :)<x(3:end, :); 0015 index = 0*x; 0016 index(2:end-1, :) = b1 & b2; 0017 0018 if plotOpt, 0019 time = 1:length(x); 0020 plot(time, x, time(logical(index)), x(logical(index)), 'ro'); 0021 end 0022 0023 % ====== Self demo 0024 function selfdemo 0025 t = 0:0.2:2*pi; 0026 x = sin(t)+randn(size(t)); 0027 x = x(:); 0028 index = feval(mfilename, x, 1);