How can I separate similar values in an array?

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

Réponses (2)

Cedric
Cedric le 21 Avr 2013
Modifié(e) : Cedric le 22 Avr 2013
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 commentaire

Arielle Clute
Arielle Clute le 22 Avr 2013
Thank you so much, I am defining a threshold manually so your code works perfectly!

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 22 Avr 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.

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by