vendredi 9 janvier 2015

K-Means Clustering of random numbers in Matlab

I have a program that generates 10 fixed points and 3 random points when run. I would like the 10 fixed points to use K-means clustering but don't know where to begin. My code is below



function TESTING (re_point)

%***********************NOTE*************************************
% if re_point = 0 [default]
% points generated for xtemp and y_temp remain fixed
% if re_point = 1
% new points are generated for x_temp and y_temp

% Variable definitions for tags and figure window
persistent xtemp ytemp hFig

% Initialisiation of re_point
if nargin<1
re_point = 0; % If 0, the points are fixed, if 1 they move
end

A1 = 30; % area defined as 30 X 30 grid
N = 10;
R = 3; % 3 readers
s = rng; % fixed tags does not change position when simulated repatedly
rng(s)

if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
% Generate x and y position of tags
xtemp = A1*rand(1,N);
ytemp = A1*rand(1,N);
end
if isempty(hFig)
hFig = figure;
end

% Generate x and y position of red points
xtemp_2 = A1*rand(1,R);
ytemp_2 = A1*rand(1,R);

% plot data
plot(xtemp,ytemp,'.',xtemp_2,ytemp_2,'rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',14);

% Labelling of the red markers
for iter = 1:numel(xtemp_2)
text(xtemp_2(iter),ytemp_2(iter), num2str(iter),...
'FontSize',8,'HorizontalAlignment','center',...
'Color','White','FontWeight','bold');
end

grid on
hold off
axis([0 A1 0 A1])

% Tag formatting
xoffset = 0;
yoffset = -1;
fsize = 8;
temp_str = mat2cell(num2str([xtemp(:) ytemp(:)], '(%.2f,%.2f)'), ones(1,N));
text(xtemp+xoffset, ytemp+yoffset, temp_str,'fontsize', fsize)

% distance function calculator
cDistance = distanceCalc()

function S = distanceCalc
S = size(numel(xtemp),numel(xtemp_2));
for ri = 1:numel(xtemp)
for fi = 1:numel(xtemp_2)
S(ri,fi) = pdist([xtemp(ri),ytemp(ri);...
xtemp_2(fi),ytemp_2(fi)],...
'euclidean');
end
end
end

end


This particular snippet from the block above generates the 10 fixed points that need to be clustered



if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
% Generate x and y position of tags
xtemp = A1*rand(1,N);
ytemp = A1*rand(1,N);
end




Aucun commentaire:

Enregistrer un commentaire