lvqcode.m

来自「这里包含了聚类的工具箱还有很详细的文档说明」· M 代码 · 共 34 行

M
34
字号
function [centro,nvoro] = lvqcode(X,centri,iter)

[N,d] = size(X);
[M,dc] = size(centri);

if dc ~= d
    disp('Dimensions from data and centroids is not equal?!')
    return;
end

% d = data dimension
% N = number of points
% L = number of iterations

centro = centri;

alph = 0.5/iter;

[dummy,pos] = sort(rand(1,N));
%pos = 1:N;
nvoro = zeros(M,1);
for n = 1:N

    datum = X(pos(n),:);

    diff = sqrt(sum((centro - ones(M,1)*datum).^2,2));
    minpos = find(diff==min(diff));
    minpos = minpos(1); % case there are two centroids, we choose the first one
    nvoro(minpos) = nvoro(minpos) + 1;

    centro(minpos,:) = (1-alph)*centro(minpos,:) + alph*datum;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?