VLFeat offers a hierarchical version of integer k-means, which recursively apply \cmd{ikmeans} to compute finer and finer partitions. We demonstrates this by:

K        = 3 ;
nleaves  = 100 ;
data     = uint8(rand(2,10000) * 255) ;
datat    = uint8(rand(2,100000)* 255) ;
[tree,A] = vl_hikmeans(data,K,nleaves) ;
AT       = vl_hikmeanspush(tree,datat) ;

Here nleaves is the desired number of leaf clusters. This is obtained by subdividing recursively each cluster in k sub-clusters. Therefore depth = floor(log(K nleaves)) recursive subdivisions are sufficient and the algorithm terminates with clusters arranged in a k-tree of of depth depth which has at least nleaves leaves (but it may have more).

Hierarchical integer K-means. Left: A depiction of the recursive clusters. Each node is a cluster center. The root note is not depicted (its center would be the mean of the dataset). Right: Clusters are represented as different colors (here are more than 100 clusters, but only tree colors are used).

The output tree is a MATLAB structure representing the tree of clusters:

> tree
tree =
 
          K: 3
      depth: 5
    centers: [2x3 int32]
        sub: [1x3 struct]

The field centers is the matrix of the cluster centers at the root node. If the depth of the tree is larger than 1, then the field sub is a structure array with one entry for each cluster. Each element is in turn a structure such as

> tree.sub
ans = 

1x3 struct array with fields:
    centers
    sub

with the cluster centers for that recursive partitions and a field erb$sub$ for the recursive children. When there are no children, this field is equal to the empty matrix

> tree.sub(1).sub(1).sub(1).sub(1)

ans = 

    centers: [2x3 int32]
        sub: []