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

回答 (2 件)

Cedric
Cedric 2013 年 4 月 21 日
編集済み: Cedric 2013 年 4 月 22 日

0 投票

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 件のコメント

Arielle Clute
Arielle Clute 2013 年 4 月 22 日
Thank you so much, I am defining a threshold manually so your code works perfectly!

サインインしてコメントする。

Image Analyst
Image Analyst 2013 年 4 月 22 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2013 年 4 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by