function [ purity ] = ClusteringPurity( true_clustering, test_clustering ) %ClusteringPurity Calculate the purity of a clustering % Detailed explanation goes here num_clusters = max(test_clustering); num_correct = 0; %number of correctly assigned samples for i = 1 : num_clusters current_true = true_clustering(test_clustering == i); %check the true clustering of all the samples clustered in cluster i majority = mode(current_true); %determine the true clustering by selecting the most frequent "true cluster" in cluster i num_correct = num_correct + sum(current_true == majority); %the number of correctly clustered samples in cluster i is the number of appearences of the majority end purity = num_correct / size(true_clustering,1); %purity is defined as the percentage of correctly clustered samples out of all samples end