function dist=cluster_dist(cluster1,cluster2,num_cluster1,num_cluster2,in_mode,out_mode)

%  in_mode: mode for measuring the distance between bags: 1 for minimum Hausdorff; 2 for maximum Hausdorff; 3 for average distance
%  out_mode: mode for measuring the distance between clusters: 1 for minimum Hausdorff; 2 for maximum Hausdorff; 3 for average distance

   distance=zeros(num_cluster1,num_cluster2);
   for i=1:num_cluster1
       for j=1:num_cluster2
           if(in_mode==1)
               distance(i,j)=minHausdorff(cluster1{i,1},cluster2{j,1});
           else
               if(in_mode==2)
                   distance(i,j)=maxHausdorff(cluster1{i,1},cluster2{j,1});
               else
                   distance(i,j)=aveHausdorff(cluster1{i,1},cluster2{j,1});
               end
           end
       end
   end  
   
   if(out_mode==1)
       dist=min(min(distance));
   else
       if(out_mode==2)
           dist1=max(min(distance));
           dist2=max(min(distance'));
           dist=max(dist1,dist2);
       else
           dist=sum(sum(distance))/(num_cluster1*num_cluster2);
       end
   end