1

In matlab, if I have a code which draws a circle and generates 100 random points inside it. I want to use k means to cluster these 100 points into 30 clusters with a circle around each cluster to differentiate between the clusters and i want to mark the center if each cluster.this is the code of the circle and the 100 random points inside it . Any help please ?

%// Set parameters

R =250;  %// radius
C = [0 0]; %// center [x y]
N = 100;    %// number of points inside circle

%// generate circle boundary
t = linspace(0, 2*pi,100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);

%// generate random points inside it

th = 2*pi*rand(N,1);
r  = R*randnlimit(0, 1, 0.5, 1, [N 1]);

xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);



%// Plot everything
figure(1), clf, hold on
% subplot(1,N0,1);
plot(x,y,'b');
hold on
text(0,0,'C')
plot(xR,yR,'p')
axis equal
zR=cell(N,1);
for i=1:N
    zR{i,1}= [xR(i) yR(i)];
end

m=cell2mat(zR);
user3482135
  • 75
  • 1
  • 7
  • You could have mentioned, that your question is a sequel to http://stackoverflow.com/questions/22768349/how-to-make-circle-in-matlab-and-generate-random-points-inside-it – matheburg May 27 '14 at 10:34

1 Answers1

1
function clusterCircle()
    %// Set parameters
    R = 250;   %// radius
    C = [0 0]; %// center [x y]
    N = 100;   %// number of points inside circle
    k = 30;    %// number of clusters 

    %// generate circle boundary
    t = linspace(0, 2*pi, 100);
    x = R*cos(t) + C(1);
    y = R*sin(t) + C(2);

    %// generate random points inside it
    th = 2*pi*rand(N,1);
    r  = R*randnlimit(0, 1, 0.5, 1, [N 1]);

    xR = r.*cos(th) + C(1);
    yR = r.*sin(th) + C(2);

    %// some simple k-means:
    % initial centroids:
    %  -> use different method, if k > N
    %  -> can be done more reasonable (e.g. run k-Means for different
    %     seeds, select seeds equidistant, etc.)      
    xC = xR(1:k)';
    yC = yR(1:k)';
    % run:
    clusters = zeros(N,1);
    clusters_old = ones(N,1);
    while sum((clusters - clusters_old).^2) > 0
        clusters_old = clusters;
        [~,clusters] = min((bsxfun(@minus,xR,xC)).^2 + ...
                           (bsxfun(@minus,yR,yC)).^2 , [] , 2);
        for kIdx = 1:k
            xC(kIdx) = mean(xR(clusters==kIdx));
            yC(kIdx) = mean(yR(clusters==kIdx));
        end
    end

    %// Plot everything
    figure(1);
    clf;
    hold on;
    %  -> plot circle and center
    text(C(1),C(2),'C');
    plot(x,y,'k');
    %  -> plot clusters
    co = hsv(k);
    for kIdx = 1:k
        % plot cluster points
        plot(xR(clusters==kIdx),yR(clusters==kIdx),'p','Color',co(kIdx,:));
        % plot cluster circle
        maxR = sqrt(max((xR(clusters==kIdx)-xC(kIdx)).^2 + ...
                        (yR(clusters==kIdx)-yC(kIdx)).^2));
        x = maxR*cos(t) + xC(kIdx);
        y = maxR*sin(t) + yC(kIdx);
        plot(x,y,'Color',co(kIdx,:));
        % plot cluster center
        text(xC(kIdx),yC(kIdx),num2str(kIdx));
    end
    axis equal    
end

%// creates random numbers, not optimized!
function rn = randnlimit(a,b,mu,sigma,sz)    
    rn = zeros(sz);
    for idx = 1:prod(sz)
        searchOn = true;
        while searchOn
            rn_loc = randn(1) * sigma + mu;
            if rn_loc >= a && rn_loc <= b
                searchOn = false;
            end
        end
        rn(idx) = rn_loc;
    end
end

enter image description here

matheburg
  • 2,097
  • 1
  • 19
  • 46