Dear Sir/Madam/Friends,
Sir, i have written code for first step of euclidean distance . now i need to merge the smallest distance points as single point. The class which i have written is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
/// <summary>
/// Summary description for CreatingDistanceMatrix
/// </summary>
public class CreatingDistanceMatrix
{
DataSet ds;
DataTable dtsource;
public ArrayList p1, p2;
public DataTable dt;
public double smallval;
public CreatingDistanceMatrix(DataSet ds)
{this.ds = ds;
dtsource = ds.Tables[0];
CrateDisMat();
GetPair();
}
private void GetPair()
{
p1 = new ArrayList();
p2 = new ArrayList();
p1.Clear();
p2.Clear();
double temp1 = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = i; j < dt.Columns.Count; j++)
{
temp1 = Convert.ToDouble(dt.Rows[j]);
if (temp1 == smallval)
{
p1.Add(i);
p2.Add(j);
}
}
}
}
private void CrateDisMat()
{
dt = new DataTable();
smallval = 0;
double temp, temp1;
bool flage = true;
double sum = 0;
for (int i = 0; i < dtsource.Rows.Count; i++)
{
DataColumn dc = new DataColumn(i.ToString(), typeof(double));
dt.Columns.Add(dc);
}
for (int i = 0; i < dtsource.Rows.Count; i++)
{
//DataRow dr = new DataRow(i);
dt.Rows.Add(i);
}
for (int i = 0; i < dtsource.Rows.Count; i++)
{
for (int j = 0; j < dtsource.Rows.Count; j++)
{
//sum = 0;
for (int k = 0; k < dtsource.Columns.Count; k++)
{
temp = Convert.ToDouble(dtsource.Rows[k]) - Convert.ToDouble(dtsource.Rows[j][k]);
temp = temp * temp;
sum = sum + temp;
}
temp1 = Math.Sqrt(sum);
if (flage && temp1 != 0)
{
smallval = temp1;
flage = false;
}
if (temp1 != 0 && smallval > temp1 && !flage)
{
smallval = temp1;
}
sum = 0;
dt.Rows[j] = temp1;
}
}
}
}
Out put of my code i have given in attachfiles now i need to merge points (0,1) as single point and in the second step (2,3) as single point. From the new point again i need to calculate Distance.
Sir, Please Kindly help me out that how i can merge two datapoints as single point.
1 2 3 4 5 6
1 0 1.4142 13.4536 14.8660 25 28.6530
2 1.4142 0 12.0415 13.4536 23.6008 27.2946
3 13.4536 12.0415 0 1.4142 11.6619 15.8113
4 14.8660 13.4536 1.4142 0 10.2956 14.5602
5 25 23.6008 11.6619 10.2956 0 5.0990
6 28.6530 27.2946 15.8113 14.5602 5.0990 0
Data set
ID A B
1 5 5
2 6 6
3 15 14
4 16 15
5 25 20
6 30 19
First step Euclidean Distance.
After merging (1,2) and (3,4) the resultant table looks like this
(1,2) (3,4) 5 6
(1,2) 0 13.4536 24.3004 27.9735
(3,4) 13.4536 0 10.9787 15.1857
5 24.3004 10.9787 0 5.0990
6 27.9735 15.1857 5.0990 0
So please help me out that how to merge two points as single point.