资源简介
简单且容易理解的k-medoids聚类算法matlab源代码
代码片段和文件信息
function [indscidx] = kmedoids(Dk)
% [indscidx] = kmedioids(Dk)
%
% Performs k-mediods clustering; only requires a distance matrix D and
% number of clusters k. Finds cluster assignments “inds“ to minimize the
% following cost function:
% sum(D(inds==iinds==i)2) summed over i=1:k
% Determining cluster assignments and cluster centers are both done in an
% efficient vectorized way. Cluster assignment is O(nk) and cluster
% centering is O(k*(max cluster size)^2)
%
% INPUTS
% D: nxn all-pairs distance matrix
% k: number of clusters
%
% OUTPUTS
% inds: nx1 vector of assignments of each sample to a cluster id
% cidx: kx1 vector of sample indices which make up the cluster centers
%
% DEMO
% Run with no arguments for demo with 2d points sampled from 3 gaussians
% using the gmdistribution function from the stats toolbox
% Written by Ben Sapp September 2010
% benjamin.sapp@gmail.com
if nargin == 0
demo();
return;
end
n = size(D1);
% randomly assign centers:
cidx = randperm(n);
cidx = sort(cidx(1:k));
iter = 0;
while 1
inds = assign_pts_to_clusters(Dcidx);
[cidxenergy_next] = update_centers(Dindscidxk);
if iter>0 && energy_next == energy
break;
end
energy = energy_next;
fprintf(‘iter: %04d energy: %.02f\n‘iterenergy)
iter = iter+1;
end
function inds = assign_pts_to_clusters(Dcidx)
S = D(cidx:);
[valsinds] = min(S[]1);
function [cidxenergy] = update_centers(Dindscidxk)
energy_next = nan(k1);
for i=1:k
indsi = find(inds==i);
[energy_next(i)minind] = min(sum(D(indsiindsi)2));
cidx(i) = indsi(minind);
end
energy = sum(energy_next);
function demo()
% problem params
k = 3;
n = 2000;
MU = [1 2;-1 -2; 3 0];
SIGMA = cat(3[2 0;0 .5][1 0;0 1] eye(2));
p = ones(13)/3;
obj = gmdistribution(MUSIGMAp);
pts = random(objn)‘;
%form all-pairs distance matrix in an efficient way
X = pts‘;
temp = sum(X.^22);
X=sqrt(2)*X;
D=-X*X‘;
D=bsxfun(@plusDtemp);
D=bsxfun(@plusDtemp‘);
%run kmedioids
[indscidx] = kmedioids(Dk);
%display
clf hold on axis square
c = lines(k);
for i=1:k
ptsi = pts(:inds==i);
ctrpt = pts(:cidx(i));
plot(ptsi(1:)ptsi(2:)‘.‘‘color‘c(i:))
plot(ctrpt(1)ctrpt(2)‘kx‘‘markersize‘22‘linewidth‘6)
end
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2289 2014-04-23 02:13 kmedoids\kmedoids.m
目录 0 2014-04-27 04:14 kmedoids\
- 上一篇:用于压缩感知的各种稀疏变换基和观测矩阵
- 下一篇:仿真2×1瑞利信道
相关资源
- Chameleon变色龙层次聚类算法实现
- 平均电流控制的boost pfc仿真(BoostPF
- haar-like特征的提取代码
- 基于机器视觉的零件缺陷检测
- 用于图像处理的三类边缘保护滤波器
- 永磁同步电机的滑模变结构(SMC)仿
- 典型雷达信号的侦察分析和雷达干扰
- cacfar单元平均恒虚警检测方法
- matlab实现阈值图像分割
- 最近邻域标准滤波器(NNSF)和概率数
- matlab有限元格式求解二维热传导(h
- matlab实现的利用backstepping算法设计的
- matlab实现的三维UWBTDOA AOA联合定位算法
- 异步电机 matlab仿真模型
- 模拟SIR信息传播模型的CP过程的仿真程
- matlab实现ID3 决策树算法
- matlab实现的标准大气模型
- Gammatone人耳滤波器
- MATLAB信号处理 、频谱分析、汉宁窗函
- fattal算法的梯度域实现色调映射(G
- 贝叶斯网络(BNT)结构和参数的学习
- matlab实现的对统计数据进行威布尔分
- 数字下变频器的matlab仿真
- QR识别和译码matlab程序
- 拉曼光纤激光器超连续谱(superconti
- matlab实现混合蛙跳算法(SFLA)代码
- SOMP算法代码
- LTE energy and spectrum efficiency
- 信号的功率谱香农熵和功率谱指数熵
- LMS RLS CMA 自适应均衡算法matlab仿真
评论
共有 条评论