If you have the Statistics and Machine Learning Toolbox (for pdist2) and the Image Processing Toolbox (for bwlabel), you can do this:
m = [...
7340.1
7340.3
7340.6
7349.0
7349.4
7358.0
7358.1
7358.2
7358.7]
m = sort(m, 'ascend')
distances = pdist2(m, m)
within2 = distances > 0 & distances < 2
numElements = numel(m);
t = logical(triu(ones(numElements, numElements), 0))
within2(t) = 0
[labeledGroups, numGroups] = bwlabel(within2)
for k = 1 : numGroups
[rows, columns] = find(labeledGroups == k);
indexes = unique([rows, columns]);
groups{k} = m(indexes);
end
celldisp(groups);
It gives you your desired result. Should work for other arrays also, though I didn't test it with any others.
Best Answer