function [z,t]=hierclus(data,distmetric,linkmethod);
distmetric,linkmethod,
%Possible distance metrics:
%'euclid'    --- Euclidean metric
%       'seuclid'   --- Standardized Euclid metric
%      'cityblock' --- City Block metric
%       'mahal'     --- Mahalanobis metric
%       'minkowski' --- Minkowski metric
switch distmetric
case 'Euclidean',
    pointdist = pdist(data,'euclid');
case 'Minkowski',
    pointdist = pdist(data,'minkowski',1);
end;

%Select linkage method
%'single'   --- nearest distance
%       'complete' --- furthest distance
%       'average'  --- average distance
%       'centroid' --- center of mass distance (approximate, computed
%                      using a formula that is exact if Y contains
%                      Euclidean distances)
%       'ward'     --- inner squared distance
switch linkmethod
case 'single',
    z=linkage(pointdist,'single');
case 'complete',
    z=linkage(pointdist,'complete');
case 'average',
    z=linkage(pointdist,'average');
case 'centroid',
    z=linkage(pointdist,'centroid');
case 'ward',
    z=linkage(pointdist,'ward');
end;

% create dendrogram and cluster the data based on the distance metric
figure;dendrogram(z,0);% set to display all nodes
title(['Hierarchical clustering: Dist: ',distmetric,' Link:',linkmethod]);

t= cluster(z,3);
