Easy clustering of a vector into groups

Automatic find peaks and associate each point to a peak in a vector
785 Downloads
Updated 12 Nov 2013

View License

function [peaks,groups,criterion] = peaksandgroups(V,select,display)
% -------------------------------------------------------------
% Find peaks and link each data point to a peak, in effect clustering the data into groups
% ---------------------------------------------
% This function looks for peaks in the data using the LAZYCLIMB method.
% This won't help you much, since lazyclimb is a name I just made up for
% this algorithm.
%
% input :
% * V : data, a vector of length N
% * select : either:
% - select >1 : the number of peaks and groups to detect
% - 0<select<1 : the threshold to apply for finding peaks
% the closer to 1, the less groups, the closer to 0, the more groups
% * display : whether or not to display a figure for the results. 0 by
% default
% * ... and that's all ! that's the cool thing about the algorithm =)
%
% outputs :
% * peaks : indices of the peaks.
% * groups: the group each point is assigned to. Nx1 vector
% * criterion : the value of the computed criterion. Nx1 vector. A high
% value indicates a point which is likely to be a peak
%
% The algorithm goes as follows:
% 1°) Sort data in decreasing order, keep corresponding positions as order
% 2°) for all pos=1:n,
% * set order(pos) as the current point (from bigger to smaller)
% * look for the closest already processed points, at left and right
% * for each of them, compute a cost and select the one with lower cost
% as the father of current. The cost corresponds to the area of the
% difference between straight line and actual observed curve. The
% rationale is that if you've got a peak, it would be tiring to go from
% it to the place you were before, and this is measured as the
% difference with a straight line.
% 3°) This gives you a criterion for all point, as well as the father of
% each point
% 4°) Threshold the criterion so that you either got a given number of
% peaks, or so that you got few or many peaks
% 5°) Propagate the information to all points so that each point is given a
% group number
%
% I don't know if that kind of algorithm has already been published
% somewhere, I coded it myself and it works pretty nice, so.. enjoy !
% If you find it useful, please mention it in your studies =)
%
% running time IS PRETTY SLOW, due to the identification of the closest
% already processed points on left and right, and to the computation of the
% criterion. If you got some nice ways to have it go faster, please tell
% me.
% ---------------------------------------------------------------------
% (c) Antoine Liutkus, 2013
% ---------------------------------------------------------------------

Cite As

Antoine Liutkus (2024). Easy clustering of a vector into groups (https://www.mathworks.com/matlabcentral/fileexchange/44290-easy-clustering-of-a-vector-into-groups), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0