www.pudn.com > EXP4.zip > e4_1.cpp, change:2015-11-26,size:5874b

```#include <stdio.h>
#include <math.h>

//Target: 用于对二维、三维向量，用不同距离准则分为两类
//Author:
//Date:   2015/11/12

//3 Dimension
// First group
/*
double m_x[20] = {6,12,18,11,9,8,7,1,15,7,10,11,11,15,4,0,18,12,15,17};
double m_y[20] = {8,13,15,3,2,15,9,11,18,2,18,17,6,6,13,12,5,4,4,15};
double m_z[20] = {5,1,8,18,6,3,14,2,2,18,4,3,4,13,16,18,7,16,5,15};

// Second group
double m_x[20] ={6,17,2,5,4,13,16,17,7,15,3,1,2,3,15,5,18,17,0,14};
double m_y[20] ={10,13,15,3,6,17,15,16,19,9,11,19,12,6,8,11,7,7,14,2};
double m_z[20] ={0,16,10,18,10,11,11,14,3,7,0,7,18,0,8,10,4,7,15,17};

// Third group
double m_x[20] ={7,12,14,8,16,14,15,11,8,17,4,17,19,9,7,10,12,15,11,2};
double m_y[20] ={14,14,19,4,4,16,13,14,5,3,15,0,4,4,2,3,16,18,15,6};
double m_z[20] ={17,8,7,13,12,2,16,6,5,5,16,3,18,7,9,3,9,2,19,19};

// Fourth group
double m_x[20] ={13,18,18,16,2,7,19,8,18,19,12,4,18,4,8,11,2,0,13,4};
double m_y[20] ={8,0,13,7,15,19,11,0,5,12,18,8,7,19,16,8,3,10,9,17};
double m_z[20] ={10,0,7,7,17,5,2,1,2,2,15,1,6,6,7,1,0,10,13,14};

// Fifth group
double m_x[20] ={0,10,17,10,3,11,5,13,2,3,16,7,1,16,1,4,9,17,4,5};
double m_y[20] ={13,0,17,9,8,4,10,11,7,0,14,0,15,3,16,15,4,10,11,2};
double m_z[20] ={11,17,17,15,9,0,11,0,12,9,14,19,4,10,18,8,2,5,14,11};

// 2 Dimension

//第一组
double m_x[20] ={14,3,15,2,5,8,19,18,4,2,4,11,3,16,1,4,9,17,4,15};
double m_y[20] ={17,19,1,11,13,7,17,12,0,0,17,18,18,3,16,15,4,10,11,2};
double m_z[20];

//第二组
double m_x[20] ={13,12,5,16,13,6,6,15,14,19,2,19,17,19,8,5,0,17,12,9};
double m_y[20] ={2,14,10,5,19,8,3,1,19,11,0,2,19,8,8,19,17,18,8,18};
double m_z[20];

//第三组
double m_x[20] ={10,2,18,16,9,14,5,2,13,14,5,9,7,4,8,9,2,7,11,14};
double m_y[20] ={4,13,1,17,17,19,9,10,16,2,1,4,13,2,3,5,13,7,15,17};
double m_z[20];

//第四组
double m_x[20] ={1,10,4,10,6,19,6,8,11,5,7,15,5,9,0,1,9,4,9};
double m_y[20] ={12,0,2,12,1,8,0,1,2,18,18,5,18,3,8,13,6,18,18,16};
double m_z[20];
*/
//第五组
double m_x[20] ={18,9,6,5,1,13,0,0,2,12,16,12,1,7,15,12,9,16,3,13};
double m_y[20] ={10,13,3,4,0,11,11,5,2,7,19,14,6,14,13,15,12,11,18,1};
double m_z[20];

/* 已知结果的样本集*/
/*
double m_x[5] ={3,0,2,2,2};
double m_y[5] ={1,2,3,0,1};
double m_z[5] ={3,4,2,3,3};
*/

double rand_max = 10000.0;
int pointNum = 20;

double ComputeDistance(int *p,int *q,int dim,int flag)
{
double min = pow(rand_max,3.0);
double max = 0;
double distance = 0;
int i;
int j;

/* minimum distance */
if(flag==1)
{
for(i=1;i<p[0]+1;i++)
{
for(j=1;j<q[0]+1;j++)
{
if(dim==3)
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2)+pow(m_z[p[i]]-m_z[q[j]],2);
else if(dim==2)
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2);
if(distance<min)
{
min = distance;
}
}
}
return min;
}

/* the maximum distance */
else if(flag==2)
{
for(i=1;i<p[0]+1;i++)
{
for(j=1;j<q[0]+1;j++)
{
if(dim==3)
{
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2)+pow(m_z[p[i]]-m_z[q[j]],2);
}
else if(dim==2)
{
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2);
}
if(distance>max)
{
max = distance;
}
}
}
return max;
}

/* mean distance */
else if(flag==3)
{
double sum = 0; // the total distance

for(i=1;i<p[0]+1;i++)
{
for(j=1;j<q[0]+1;j++)
{
if(dim==3)
{
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2)+pow(m_z[p[i]]-m_z[q[j]],2);
}
else if(dim==2)
{
distance = pow(m_x[p[i]]-m_x[q[j]],2)+pow(m_y[p[i]]-m_y[q[j]],2);
}
sum = sum+sqrt(distance);
}
}
return sum/(p[0]*q[0]);
}
}

void main()
{

int classNum = 2;
int dim;
int flag;
printf("Please choose the criterion:\n1. Minimum Distance\n2. Maximum Distance\n3. Mean Distance\n");
scanf("%d",&flag);
printf("\nPlease choose the feature vector dimension (2/3):\n");
scanf("%d",&dim);

int * code;
int * * cla;
code = new int [pointNum*(pointNum+1)];// creat a space to store the sample label of each cluster
cla = new int *[pointNum];
/* make each row of cla points each row of code,
though code is a 1 dimension array*/
int index = 0;
int i;
int j;
int k;
int p; // interation index
double distance;
for (i=0;i<pointNum;i++)
{
cla[i] = &code[index];
index = index+(pointNum+1);
}

// initiate the array cla, every cluster has a point at first
for(i=0;i<pointNum;i++)
{
cla[i][0] = 1;//at first, every cluster has a point
cla[i][1] = i;//each sample ponint represent a cluster

}
/* Clustering*/

for(i=0;i<pointNum-classNum;i++) // iteration size is pointNum-classNum
{
double min = pow(rand_max,3.0);
int c1,c2;

for(j=0;j<pointNum-1;j++)
{
for(p=j+1;p<pointNum;p++)
{
if(cla[j][0]>0&&cla[p][0]>0)
{
distance = ComputeDistance(cla[j],cla[p],dim,flag); // pass the jth cluster and pth cluster to calculate distance
if(distance<min)
{
min = distance;
c1 = j;
c2 = p;
}
}

}
}

/* merge the two clusters that have mininest distance*/
for (k=0;k<cla[c2][0];k++)
{
cla[c1][cla[c1][0]+k+1] = cla[c2][k+1]; // update sample point label c1 has, that is, replace the sample in c2 to c1
}
cla[c1][0] = cla[c1][0]+cla[c2][0];	// update point numbers that c1 cluster has
cla[c2][0] = 0; // destroy the cluster c2
}

/* display the clustering results */
k = 1;
printf("\nResult:(Cluster, point No)\n");
for(i=0;i<pointNum;i++)
{
if(cla[i][0]>0)
{
printf(" Cluster No.%d\n",k);
k++;
for(j=1;j<cla[i][0]+1;j++)
{
printf("%d\t",cla[i][j]+1);
}
printf("\n\n");
}
}
}

```