排序数组产生零的数组c#

时间:2014-06-16 00:44:37

标签: c# arrays sorting

我遇到了在C#中排序双数组的问题。它有一些值,但排序后都是零。

这是代码,我真的不知道为什么我的sort函数会返回所有零。

@EDIT添加调用med(double[])

的整个函数
daneTab is DataTable with double values

private void kmedi_but_Click(object sender, RoutedEventArgs e)
    {
        int liczbakolumn = Int32.Parse(k_grupowanie_kolumny.Text);
        int k = Int32.Parse(k_grupowanie.Text);
        double[][] punkty = new double[k][];
        for(int i=0;i<k;i++)
            punkty[i] = new Double[liczbakolumn];
        double[][] daneP = przepiszNaTabliceJ(liczbakolumn);
        double[] tablicaKlas = new double[daneTab.Rows.Count];
        double min = double.MaxValue;
        int klasatemp = -1;
        int odleglosc = wybor_grupowanie.SelectedIndex;
        int[] ileDanejKlasy = new int[k];

        Random r = new Random();
        for (int i = 0; i < k; i++)
        {
            int n = r.Next(daneTab.Rows.Count);
            for (int j = 0; j < liczbakolumn; j++)
            {
                punkty[i][j] = daneP[n][j];
            }
        }

        for (int i = 0; i < daneP.Length; i++)
        {
            if(odleglosc == 0)
            {
                for (int j = 0; j < k; j++)
                {
                    double tmp = Distance.Euclidean(daneP[i], punkty[j]);
                    if (tmp < min)
                    {
                        min = tmp;
                        tablicaKlas[i] = j;
                        ileDanejKlasy[j]++;
                    }
                }
                min = double.MaxValue;
            }
            else if(odleglosc == 1)
            {
                for (int j = 0; j < k; j++)
                {
                    double tmp = Distance.Manhattan(daneP[i], punkty[j]);
                    if (tmp < min)
                    {
                        min = tmp;
                        tablicaKlas[i] = j;                            
                        ileDanejKlasy[j]++;
                    }
                }
                min = double.MaxValue;
            }
            else if(odleglosc == 2)
            {
                for (int j = 0; j < k; j++)
                {
                    double tmp = Distance.Chebyshev(daneP[i], punkty[j]);
                    if (tmp < min)
                    {
                        min = tmp;
                        tablicaKlas[i] = j;
                        ileDanejKlasy[j]++;
                    }
                }
                min = double.MaxValue;
            }
            else if(odleglosc == 3)
            {
                //for (int j = 0; j < k; j++)
                //{
                //    //double tmp = Distance.Mahalanobis(daneP[i], punkty[j]);
                //    if (tmp < min)
                //    {
                //        min = tmp;
                //        tablicaKlas[i] = j;
                //    }
                //}
                min = double.MaxValue;
            }
        }

        for (int i = 0; i < k; i++)
        {
            double[][] domedian = new double[liczbakolumn][];
            for (int d = 0; d < liczbakolumn; d++)
                domedian[d] = new double[ileDanejKlasy[d]];
            int m = 0;
            for (int j = 0; j < daneP.Length; j++)
            {                    
                if (tablicaKlas[j] == i)
                {
                    for (int l = 0; l < liczbakolumn; l++)
                    {
                        domedian[l][m] = daneP[j][l];
                    }
                    m++;
                }
            }
            for (int l = 0; l < liczbakolumn; l++)
            {
                punkty[i][l] = med(domedian[l]);
            }
        }
        int g;
    }

    public double med(double[] tab)
    {

        Array.Sort(tab, dobles);
        double mediana;
        if (tab.Length % 2 == 0)
        {
            mediana = (tab[(tab.Length / 2) - 1] + tab[tab.Length / 2]) / 2;  
        }
        else
        {
            mediana = tab[(tab.Length / 2) - 1];
        }

        return mediana;
    }

    private double[][] przepiszNaTabliceJ(int liczbakolumn)
    {
        double[][] nowa = new double[daneTab.Rows.Count][];
        for (int i = 0; i < daneTab.Rows.Count;i++)
        {
            nowa[i] = new double[liczbakolumn];
        }
        int j = 0;
        foreach (DataRow r in daneTab.Rows)
        {
            for (int i = 0; i < liczbakolumn; i++)
            {
                nowa[j][i] = Double.Parse(r.ItemArray[i].ToString());
            }
            j++;
        }
        return nowa;
    }        
}

1 个答案:

答案 0 :(得分:2)

此代码无法编译。您无法返回double[],就好像它是double

如果排序后值全部为零,那么在排序之前它们必须为零。