欧几里得基于距离的聚类与预定数量的成员

46个视图(30天)
你好你所有
包含点我有一个数据点的二维坐标,我想这些点集群基于它们之间的最小距离的K组。每个集群都有预定数量的成员,例如,五名成员,就像下面的图片。注意保持数据点,将未聚集。
在Matlab函数,帮助我吗?
2的评论
穆罕默德zahiri
穆罕默德zahiri 2020年6月4日
编辑:穆罕默德zahiri 2020年6月4日
我想只有一定数量的点,是相邻的集群和其余的点保持不变。
这些函数可以这样做吗?

登录置评。

接受的答案

图像分析
图像分析 2020年6月4日
You could easily write your own loop to do it. Just start with the closest pair of points and keep assigning nearby neighbors to that cluster until you reach the required number of neighbors in the cluster. Then increment the cluster number of repeat to find the next cluster. Keep going until all points are gone (used up).
clc;%清除命令窗口。
流(“开始运行% s.m。\ n”,mfilename);
关闭所有;%关闭所有数据(除了imtool。)
清晰;%擦掉所有现有的变量。或者clearvars如果你想要的。
工作空间;%确定工作区面板显示。
格式长g;
格式紧凑的;
字形大小= 22;
numPoints = 200;
pointsPerCluster = 5;
numClusters =装天花板(numPoints / pointsPerCluster)
坐标= 0 (pointsPerCluster 2 numClusters);
表(t = 0 (numPoints, 1), 0 (numPoints, 1), 0 (numPoints, 1), 0 (numPoints, 1),“VariableNames”,{“ClusterNumber”,“PointNumber”,“x”,“y”});
xy =兰德(numPoints, 2);
x = xy (: 1);
y = xy (:, 2);
次要情节(1、2、1);
情节(x, y,“b”。,“MarkerSize”14);
网格;
%得到每个点到其他点的距离
距离= pdist2 (xy, xy);
minDistance = min(距离(距离~ = 0))
(行,坳)=找到(距离= = minDistance)
currentRow =行(1)%得到第一点。
指针= 1;
k = 1: numClusters
%得到每个点到其他点的距离
距离= pdist2 (xy, xy (currentRow:));
%找到最接近的点。
minDistances =貂(距离、pointsPerCluster);
[ia, ib] = ismember(距离,minDistances);
行=找到(ib);
%将这些坐标存储为集群# k
n = 1:长度(行)
t.ClusterNumber(指针)= k;
t.PointNumber(指针)= n;
t.x(指针)= xy(行(n), 1);%存储x值。
t.y(指针)= xy(行(n), 2);%存储y值。
指针=指针+ 1;
结束
如果指针> = numPoints
打破;%辞职当所有点都用完了
结束
%设置当前行坐标到正无穷所以我们知道不会再考虑(使用)。
xy(行,:)=正;
%得到新的集群,新的起点。
%得到每个点到其他点的距离
距离= pdist2 (xy, xy);
minDistance = min(距离(距离~ = 0));
[行,坳]=找到(距离= = minDistance);
currentRow =行(1);%得到第一点。
结束
%显示集群独特的颜色
次要情节(1、2、2);
gscatter (t。x, t。y, t.ClusterNumber);
密切注意,点在位于集群的习惯,剩下的点可用于集群将更加分散。我认为这是显而易见的,对吧?例如如果只有2集群在一维,如果你值(1,3,5,61,62,63,64,65,99100]然后集群将# 1(61、62、63、64、65](严格的分组)和集群% 2(1、3、5,99100)(普遍的)。
5个评论
斯特凡诺chiappini
斯特凡诺chiappini 2021年8月31日
我的dubt: 1)加载数据集测试如何?
2)我运行脚本,但line44向我展示了一个错误:对接数据不支持“WindowState”。金宝app
错误dbscan_demo(44)行
hFig。WindowState =“最大化”;%可能不会在MATLAB的早期版本中工作。
我怎样才能解决这些问题呢?
我用Matlab R2021a。
非常感谢。

登录置评。

更多的答案(2)

图像分析
图像分析 2021年9月1日
斯特凡诺:
关于“如何让我所有集群在一个独特的人物情节吗?因为在您的脚本,它只显示一个集群时,我不能够保持集群的阴谋”。
把抓住后调用次要情节或情节,并带走呼吁推迟。见下面的代码。
关于“此外,我如何为每个集群独特的情节设置不同的颜色?”每个集群已经绘制在不同的颜色。按原样的演示,使用默认值,集群2有很多点,你看不到其他集群,有时只有2分。
如果这能帮助,你能 单击“接受这个答案”链接 吗?提前谢谢。
% dbscan尝试与斯特凡诺的数据图像分析
clc;%清除命令窗口。
关闭所有;%关闭所有数据(除了imtool。)
清晰;%擦掉所有现有的变量。或者clearvars如果你想要的。
工作空间;%确定工作区面板显示。
格式长g;
格式紧凑的;
字形大小= 20;
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%读入数据。
数据= readmatrix (“用法”);
x =数据(:1);
y =数据(:,2);
z =数据(:,3);
%减去意味着
x = x -意味着(x);
y = y -意味着(y);
z - z =意味着(z);
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%显示数据。
次要情节(1、2、1);
plot3 (x, y, z,“。”,“MarkerSize”3);
网格;
包含(第一列的);
ylabel (第2列的);
zlabel (第三列的);
标题(原始数据没有分类。);
hFig = gcf;
hFig。WindowState =“最大化”;%可能不会在MATLAB的早期版本中工作。
drawnow;
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%问用户搜索半径。
defaultValue = {“0.3”};
titleBar =输入搜索半径的;
userPrompt = {的输入搜索半径:};
caUserInput = inputdlg (userPrompt titleBar 1, defaultValue);
如果isempty (caUserInput),返回,结束如果他们点击取消%纾困。
%从字符串转换为浮点数。
usersValue1 = str2double (caUserInput {1})
% usersValue1检查有效性。
如果isnan (usersValue1)
%他们没有输入一个数字。
%他们点击取消,或输入一个字符,符号,或别的东西不允许的。
%将默认从一个字符串,插到usersValue1。
usersValue1 = str2double (defaultValue {1});
消息= sprintf (”我说必须是一个数字。\ nTry替换用户。倪\将使用%。2 f,继续。”,usersValue1);
uiwait (warndlg(消息));
结束
searchRadius = usersValue1;
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%与“dbscan聚类算法。
% (classNumbers corepts] = dbscan(距离、searchRadius minPointsPerCluster,“距离”,“预计”)
% searchRadius = 0.01;%是在同一个集群如果其他的点是在这个点。
minPointsPerCluster = 2;%,我们需要至少有这许多点被认为是一个有效的集群。
开始时间=抽搐;
流(“开始dbscan()函数在% s。\ n 'datestr(现在));
[classNumbers, isACorePoint] = dbscan(数据、searchRadius minPointsPerCluster);
elapsedSeconds = toc(开始时间);
流(“完成了dbscan %后()函数。1秒。\ nFound % d集群。\ n”、elapsedSeconds马克斯(classNumbers));
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% classNumbers = classNumbers - min (classNumbers) + 1%转化消极类一切积极类号码。
ucn =独特(classNumbers);
%获得颜色的列表,以便每个数据点/集群将有其自己独特的颜色。
如果长度(ucn) < = 7
colorList =线(长度(ucn));
其他的
春天colorList =(长度(ucn));
结束
%把每个点旁边的类标签。
次要情节(1、2、2);
%绘制每个类在不同的颜色。
持有;
k = 1:长度(ucn)
thisClassNumber = ucn (k);
thisClassesIndexes = classNumbers = = thisClassNumber;
xSubset = x (thisClassesIndexes);
ySubset = y (thisClassesIndexes);
zSubset = z (thisClassesIndexes);
%在课上画上所有的点。
thisColor = colorList (k,:)
plot3 (xSubset ySubset zSubset,“。”,“颜色”thisColor,“MarkerSize”1);
%的意思(重心)。
meanx =意味着(xSubset);
小气鬼=意味着(ySubset);
meanz =意味着(zSubset);
numPointsInThisCluster =长度(xSubset);
%绘制重心是黄色的。
持有;
meanz plot3 (meanx,小气鬼,“y”。,“MarkerSize”,30);
网格;
如果thisClassNumber = = 1
流(“阶级% d(不在任何集群)% d点和重心在(%。1 f %。1 f %。1f).\n'小气鬼,,numPointsInThisCluster thisClassNumber meanx meanz);
promptMessage = sprintf (这是类# % d % d。\ nThese并不在任何集群。\幼虫有% d点。\ nDo你想继续处理,\也辞职处理?”,
thisClassNumber马克斯(classNumbers) numPointsInThisCluster);
标题= sprintf (“% d孤立点不是在任何集群”,numPointsInThisCluster);
标题(标题,“字形大小”、字形大小);
其他的
流(“阶级% d % d点和重心在(%。1 f %。1 f %。1f).\n'小气鬼,,numPointsInThisCluster thisClassNumber meanx meanz);
promptMessage = sprintf (这是类# % d % d。\幼虫有% d点。\ nDo你想继续处理,\也辞职处理?”,
thisClassNumber马克斯(classNumbers) numPointsInThisCluster);
标题= sprintf (“集群% d % d点。”、numPointsInThisCluster thisClassNumber);
标题(标题,“字形大小”、字形大小);
结束
titleBarCaption =“继续?”;
buttonText = questdlg (promptMessage titleBarCaption,“继续”,“退出”,“继续”);
如果包含(buttonText“退出”,“IgnoreCase”,真正的)
打破;%或返回或继续。
结束
结束
持有;
%更新标题与集群的数量。
numClusters = (ucn > 0)之和;
numClusteredPoints =总和(isACorePoint);
numIsolatedPoints = (~ isACorePoint)之和;
标题= sprintf (“发现% d集群。在聚类质心黄斑。\ n % d点编号的集群。\ n % d红点不是在任何集群。,
numClusters、numClusteredPoints numIsolatedPoints);
标题(标题,“字形大小”字形大小,“翻译”,“没有”);
g = gcf;
g。WindowState =“最大化”;%可能不会在MATLAB的早期版本中工作。
5个评论

登录置评。


斯特凡诺chiappini
斯特凡诺chiappini 2021年8月30日
你好,我也有同样的问题3 x3矩阵对橄榄树点云。我只希望集群个体树应用“欧几里得集群”。我如何应用上述脚本?
我附上. txt文件的输入数据。
非常感谢。
6个评论
斯特凡诺chiappini
斯特凡诺chiappini 2021年9月1日
编辑:斯特凡诺chiappini 2021年9月1日
你好,谢谢你你的美妙的帮助在我的问题。因为现在我只用DBSCAN算法提供的Mathworks (https://it.mathworks.com/help/stats/dbscan.html)。这些相似的输出。我有最后一个问题:我如何使情节所有集群在一个独特的人物?因为在您的脚本,它只显示一个集群时,我不能够保持集群。此外,我如何为每个集群独特的情节设置不同颜色的?
非常感谢。

登录置评。

标签

下载188bet金宝搏

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!

翻译的