The code shown for the following array sorts it into 3 groups. It doesn't put the first number (7300.0) into a group because it would be by itself. Is there a way to alter this code so that it makes 4 groups and allows for one of the groups to have a single entry?
m = [...7300.07340.17340.37340.67349.07349.47358.07358.17358.27358.7];% First sort m so that close by ones has adjacent indexes.
m = sort(m, 'ascend')% Get distance of every element to every other element.
distances = pdist2(m, m)% Find out which pairs are within 2 of each other.
within2 = distances > 0 & distances < 2% Erase upper triangle to get rid of redundancy
numElements = numel(m);t = logical(triu(ones(numElements, numElements), 0))within2(t) = 0% Label each group with an ID number.
[labeledGroups, numGroups] = bwlabel(within2)% Put each group into a cell array
for k = 1 : numGroups [rows, columns] = find(labeledGroups == k); indexes = unique([rows, columns]); groups{k} = m(indexes);endcelldisp(groups); % Display the results in the command window.
Best Answer