Q:

Grouping similar values in Matlab

If I have an array that contains the values [6712, 7023, 7510, 7509, 6718, 7514, 7509, 6247] and I want 4 groups of similar numbers so that the output is 4 matrices:
[6247]
[6712, 6718]
[7023]
[7510, 7509, 7514, 7509]

What would be the best way to accomplish this?

A:

I believe the term you are looking for is clustering. For example, we can apply the Kmeans algorithm to group the data into 4 clusters:
X = [6712, 7023, 7510, 7509, 6718, 7514, 7509, 6247];
[IDX,C] = kmeans(X, 4, 'EmptyAction','singleton');
G = cell(4,1);
for i=1:4
    G{i} = X(IDX==i);
end 

This is one of the result I get:
>> G{:}
ans =
        7510        7509        7514        7509
ans =
        7023
ans =
        6247
ans =
        6712        6718

Usually this works best with more points (also works for multidimensional data)

