Grouping based on similarity and indices

29 views (last 30 days)
I have an array A of size 300*1 which has a lot of repeating values. These values are the angular position of a point on a circle so these values range between (0,360). Is there a function in matlab that can group similar values present in array A and return the indices of these values. With these indices I can average data in another array B of similar size.

Accepted Answer

John BG
John BG on 22 Dec 2016
[v_sorted i_sort]=sort(v)
in v_sorted same angles are together, all values sorted in ascending order
i_sort contains the permuted indices so that
v_sorted=v(i_sort)
if you find my answer useful would you please mark it as Accepted Answer by clicking on the ACCEPT ANSWER button?
thanks in advance for time and attention
John BG
  3 Comments
Jaime Castiblanques
Jaime Castiblanques on 12 Mar 2021
I have a similar problem. I have a matrix of 2904x3 where each column represents the x, y and z coordinates of some vectors. I need to group them in such a way that I can get several matrices of vectors which all have the same z coordinate. Any ideas?
Image Analyst
Image Analyst on 12 Mar 2021
Use findgroups(). Post your data in a new question if you cannot figure it out.

Sign in to comment.

More Answers (3)

Sean de Wolski
Sean de Wolski on 21 Dec 2016
doc discretize
Then
doc accumarray
doc splitapply

Image Analyst
Image Analyst on 22 Dec 2016
For example if you know how many groups there are, then you can use kmeans. Here's a full demo with 5 groups:
% Clean up / initialization
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
numGroups = 5
radius = 1
centerAngles = linspace(0, 330, numGroups+1)
allAngles = [];
subplot(1, 2, 1);
for g = 1 : numGroups
theseAngles = [centerAngles(g) + 30 * rand(1, 10)]';
allAngles = [allAngles; theseAngles];
% Plot
x = cosd(theseAngles);
y = sind(theseAngles);
plot(x, y, '.', 'MarkerSize', 20);
hold on
end
grid on;
axis square
title('Truth - Actual Groups', 'FontSize', fontSize);
xlim([-1.1, 1.1]);
ylim([-1.1, 1.1]);
% Now assume there are numGroups and figure out what group they belong to
% Cluster via kmeans()
groupLabels = kmeans(allAngles, numGroups);
% Plot
x = cosd(allAngles);
y = sind(allAngles);
subplot(1, 2, 2);
gscatter(x, y, groupLabels);
grid on;
axis square
title('Groups Determined by kmeans()', 'FontSize', fontSize);
xlim([-1.1, 1.1]);
ylim([-1.1, 1.1]);
The colors are different but that doesn't matter. Each group is identified by a different color. You can see that every point is grouped into the group that it should be classified into.
  4 Comments
Image Analyst
Image Analyst on 14 May 2018
Maria: below is the answer I got from the Mathworks. I am attaching the fixed version.
===============================================
I am writing in reference to your Technical Support Case #03085610 regarding 'kmeans grouping is not correct the first time'.
After further discussion with my peers, we were able to determine that the issue is arising due to the way in which "kmeans" calculates clusters. "kmeans" is an algorithm that is very sensitive to its initialization. For this particular problem it is very likely that the "kmeans" will be trapped in a local minima and cannot reach the global minima (i.e. the ground truth solution).
One approach to circumvent this issue is to repeat the clustering several times with different initializations by using the name value pair of ‘replicates’ within the "kmeans" function. Replace the line of the code where you call the "kmeans" function with the following:
>> groupLabels = kmeans(allAngles, numGroups, 'replicates',5);
Maria
Maria on 24 May 2018
Edited: Maria on 24 May 2018
Thank you very much! It's working now

Sign in to comment.


Image Analyst
Image Analyst on 21 Dec 2016
Yes, lots of them. Do you know how many clusters you will have? Have you checked out the Classification Learner app on the Apps tab? It requires the Statistics and Machine learning Toolbox. That's definitely your first place to start.

Community Treasure Hunt

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

Start Hunting!