clustering nearest 5 elements of a data

I have 36 points I need to find the nearest 5 elements of each point and cluster it in a group. Then there is a total of 36 clusters will get. The points are in the 'core' and I need to find the nearest elements from the 'data'. How can I do that? Please help me.
clc;
clear;
data=xlsread('Pimaxl.xlsx');
Si=size(data);
asc=sort(data);
num = numel(data);
diff=unique(asc);
core1=maxk(diff,12);
core2=mink(diff,12);
n = numel(diff(:));
midpoint = ceil(n/2);
core3 = diff(midpoint-5:midpoint+6);
cor=[core1 core2];
core=[cor core3];

2 Comments

core is 12x3, while data is 768x9. What is your definition of point and what is your definition of distance? Is every single element a point and is the distance the absolute difference?
Have a look at the answer I provided here
You could do the same as that but just replace min with mink. Also as pointed out in the comments a sqrt is not needed.

Sign in to comment.

 Accepted Answer

You could do the following which is to find the 5 minimum absolute differences for each element in core.
[~,idx] = mink(abs(data(:)-core(:).'),5,1);
clusters = data(idx)
Here, the input array to mink is 6912 by 36 where each row corresponds to an element in data and each column corresponds to an element in core and contains every possible absolute difference. The mink function returns an index that you can then sub directly back into data to get your clusters. Clusters is 5 by 36 with each column corresponding to the 36 values in core.

24 Comments

Thank you sir.
I tried to plot this using the plot function. Then I am getting the clusters on the left side only. Is that my plotting mistake or my code's mistake. Please help me.
plot(data)
hold on
plot(clusters)
It depends how you want to plot it, you could try this:
plot(1:36,clusters,'or')
hold on, plot(1:36,core(:),'.blue','MarkerSize',16) % shows core aswell
Has this answered your question?
Sorry for the late reply sir.
I need a picture like the below one.
Representing the cores and cluster group in the data. How can I do that? Please help me.
Thank you
Notice that here every point has two elements: the value of feature1 and the value of feature2. You need to have both to be able to create such a plot.
It's not clear to me how your data relates to the above chart. As Rik points out, you need data for both x and y axes.
Is it possible to partition the data 50:50 ? Or any other propotional level.
Sure, but only you know if that makes any sense at all. A number without interpretation is meaningless.
I need the partition only for representing the graph. How can I plot using data partition ?
Define what the x value of your points will be and what their y values will be. Then you can produce a plot.
If you want a graph in the mathematical sense, then you need to describe which nodes are connected to each other.
I used gscatter for this,but there is some problems. \
Some errors like
"Error using gscatter (line 119)
There must be one value of G for each row of X.
Error in density (line 29)
gscatter(X,Y,clusters,'rkgb','o*')"
What is the problem and how to plot this. Please help me.
X=data(1:384);
Y=data(385:768);
figure(1)
gscatter(X,Y,clusters,'rkgb','o*')
Thank you both for helping me.
There were some problems with plotting only. Hope you will help me.
Do you mean your problem is solved, or that you have a remaining error?
My grouping part is solved but the problem is in the remaining ploting part.
Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
I don't know how to use the MWE, but I will try. Thank you.
A MWE is not some kind of special tool, it is just a standalone piece of code. Have you read the wikipedia link?
Here's an example of finding the 20 nearest points to 4 different "cores" in 2D using randomly generated data. In this case, I know which data corresponds to x and y so it is very straight forward to implement.
% Generate some random data and some cluster points.
pts = [50 50; 50 150; 150 50; 150 150];
data = randi([0 200],[500 2]);
% Plot random data
hf = figure();
hp = plot(data(:,1),data(:,2),'.','MarkerSize',12);
% Plot "core points"
hold on, plot(pts(:,1),pts(:,2),'pk','MarkerFaceColor','k','MarkerSize',12)
% Find distance from a given core to each each data point (2D)
dist2 = (data(:,1) - pts(:,1).').^2 + (data(:,2) - pts(:,2).').^2;
[~,idx] = mink(dist2,20,1);
% Some adjustments to help demo
ha = hf.Children; ha.XLimMode = 'manual'; ha.YLimMode = 'manual';
% Show resulting points
hold on, plot(data(idx(:),1),data(idx(:),2),'or','MarkerFaceColor','r')
hp.Visible = 'off'; % Hide Original data plot.
Is it possible to do the samething using my dataset?
I tried this one using my dataset and core points but I am getting some errors like:
" Index in position 1 exceeds array bounds (must not exceed 768).
Error in density (line 69)
hold on, plot(data(idx(:),1),data(idx(:),2),'or','MarkerFaceColor','r') "
% Plot random data
hf = figure();
hp = plot(data(:,1),data(:,2),'.','MarkerSize',12);
% Plot "core points"
hold on, plot(core(:,1),core(:,2),'pk','MarkerFaceColor','k','MarkerSize',12)
% Find distance from a given core to each each data point (2D)
[~,idx] = mink(abs(data(:)-core(:).'),5,1);
clusters = data(idx);
% Some adjustments to help demo
ha = hf.Children; ha.XLimMode = 'manual'; ha.YLimMode = 'manual';
% Show resulting points
hold on, plot(data(idx(:),1),data(idx(:),2),'or','MarkerFaceColor','r')
hp.Visible = 'off'; % Hide Original data plot.
You should undo some of your edits: make sure you also have a separate variable that contains the distance. That way you can confirm your code results in the same shape variables as that from Turlough.
You need to explain what part of your data is x or y values and similarly what part of core is x or y values. This is absolutely required to do what I have shown in my last comment.
If I partition the data 50:50 ratio for both x and y-axis. How can I achieve a core point that has both x and y-axis(like in the above some min and max values from the data)?
Please help me.
I can't say unless you show me what your partitioned data looks like. Refer back to my previous comment.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!