How can I separate similar values in an array?
Show older comments
For example:
A = [ 44 45 44 -19 -19 -19 -18 -19 45 43 10 11]
I want the result to be
A1 = [ 44 45 44 45 43]
A2 = [ -19 -19 -18 -19]
A3 = [ 10 11]
And I would need this to be able to create an unlimited number of groups (ie. A1, A2, A3, ... , An)
Please help
Answers (2)
You can use an approach based on the following:
% - Define threshold (max diff. that doesn't break a group).
threshold = 5 ;
% - Locate boundaries.
boundaries = [true, abs(diff(A)) > threshold, true] ;
% - Convert blocks -> cells.
A_grp = mat2cell(A, 1, diff(find(boundaries))) ;
Here, A_grp is a cell array whose elements contain vectors that you named A1, A2, etc. For example:
>> A_grp{1} % What you named A1.
ans =
44 45 44
EDIT: I assumed that you could define a threshold manually; if it is not the case, jump directly to Image Analyst's answer.
1 Comment
Arielle Clute
on 22 Apr 2013
Image Analyst
on 22 Apr 2013
0 votes
You need an unsupervised clustering algorithm. Go here for a list of a bunch of clustering algorithms: http://en.wikipedia.org/wiki/Category:Data_clustering_algorithms. Or maybe you could use the mean shift algorithm.
1 Comment
Arielle Clute
on 22 Apr 2013
Categories
Find more on Color Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!