Search code examples
matlabcluster-analysisfuzzy

Matlab output each cluster from fuzzy clustering


How do you output the data from each cluster using FCM in matlab?

[center,U,obj_fcn] = fcm(data,cluster_n) 

Solution

  • I used the U vector to determine which class each datapoint belongs to. It content can be thought of as the probability for each class to belong to a class (notice then all columns sum to 1), so choosing which ever class is most probable is a reasonable approach. This is done by storing the second output argument of max().

    Below I have stated some general purpose code you can use.

    %# Start parameters and variables
    nClasses = 3;
    CM = jet(nClasses); %# Colormap for visualization of up to 255 classes
    
    %# Create dataset
    data = [mvnrnd([0 0],eye(2),100); mvnrnd([3,3],0.5*eye(2),50)];
    
    %# Cluster
    [center,U,obj_fcn] = fcm(data,nClasses); 
    
    %# Extract class assignment
    [~,y] = max(U); 
    
    %# Visualize
    f1=figure(1);clf
    plot(data(:,1),data(:,2),'.k')
    hold on
    for i = 1 : nClasses
        plot(data(y==i,1),data(y==i,2),'o','color',CM(i,:));
    end
    

    enter image description here

    EDIT:

    To extract the datapoints of one class into a new variable, simply use

    class1data = data(y==1,:);