load 'train.mat';

class_num = 40;
img_per_class = 100;

X = features(1:4000, 41:898);
Y = pdist(X);
Z = linkage(Y, 'complete');

plot(1:size(Z,1), Z(:,3));

idx = 1 : (class_num * img_per_class);
count = class_num * img_per_class;
for i = 1 : size(Z,1) - 99
    count = count + 1;
    idx(find(idx == Z(i,1))) = count;
    idx(find(idx == Z(i,2))) = count;
end

r = unique(idx);
data = zeros(length(r), class_num);
for i = 1 : length(r)
    new_idx = find(idx == r(i));
    for j = 1 : class_num
        data(i, j) = numel(find(new_idx > (j-1) * img_per_class)) ...
            - numel(find(new_idx > j * img_per_class));
    end
end