Example #1
0
        public Claster[] Clasterize(STATND statNd, int needClasterCount, IClasterMetrics D)
        {
            List <Claster> clasters = formClasterForEachPoint(statNd);

            Matrix distances = CalcMatrixOfDistances(clasters, D);

            while (clasters.Count > needClasterCount)
            {
                int[] ij = FindMinDistance(distances);
                int   l = ij[0], h = ij[1]; //l always bigger than h???

                clasters[h].AppendPointsFromClater(clasters[l]);

                distances = distances.RemoveRow(l);
                distances = distances.RemoveColumn(l);

                //go by row
                for (int m = 0; m < h; m++)
                {
                    distances[h, m] = D.LansaWilliamsDistance(clasters[l], clasters[h], clasters[m]);
                }

                //go by column
                for (int m = h + 1; m < distances.Rows; m++)
                {
                    int mReal = m >= l ? m + 1 : m; //in array of clasters
                    distances[m, h] = D.LansaWilliamsDistance(clasters[l], clasters[h], clasters[mReal]);
                }

                //it is here, cause we need in Sl, Sh, Sm for Lansa Williamsa
                clasters.RemoveAt(l);
            }

            return(clasters.ToArray());
        }
Example #2
0
        private Matrix CalcMatrixOfDistances(List <Claster> clasters, IClasterMetrics D)
        {
            int n = clasters.Count;

            double[][] distances = ArrayMatrix.GetJaggedArray(n, n);

            for (int i = 1; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (j == i)
                    {
                        break;          //дальше диагонали не считать
                    }
                    else
                    {
                        distances[i][j] = D.GetClasterDistance(clasters[i], clasters[j]);
                    }
                }
            }

            return(new Matrix(distances));
        }