2013-11-05 70 views
0

我计算了两个数组的长度总和,以获得平均值。我现在想要做的是计算数组内的数字总和,以便我可以计算标准偏差。如何计算一个数组的平均值[] []

jbtnGenerate.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ae) { 
     try { 
      // variable for the sum 
      double sum = 0; 
      // variable for standard deviation 
      double ecartTypeXbarre = 0; 
      // variable to calculate the numbers but squared 
      double sommeXcarre = 0; 
      double ecartType1 = 0; 
      double moyenneXbarre = 0;// that's the average 
      int lengths = 0; 
      // Variable for Size of Samples 
      int tailleEchantillon = Integer.parseInt(jtfn.getText()); 
      // Variable to get number of samples 
      int nombreEchantillon = Integer.parseInt(jtfNbEchantillons.getText()); 

      // Variable where i stock both previous ones 
      double echantillon[][] = new double[tailleEchantillon][nombreEchantillon]; 

      // Generates random numbers taken from data which is an ArrayList of 
      // doubles and puts it into echantillon 
      for (int i = 0; i < tailleEchantillon; i++) { 
       for (int j = 0; j < nombreEchantillon; j++) 
        echantillon[i][j] = data.get(rng.nextInt(data.size())); 
      } 

      // puts the numbers into a TextArea 
      for (int i = 0; i < tailleEchantillon; i++) { 
       for (int j = 0; j < nombreEchantillon; j++) 
        jta.append(String.valueOf(echantillon[i][j]) + "\n"); 
      } 
       //Calculating avg and sum 
      for (int i = 0; i < echantillon.length; i++) { 
        //Calculates the lengths of the first array 
        lengths += echantillon[i].length; 
//sommeXbarre is supposed to be the sum of values inside echantillon 
        sommeXbarre+= lengths; 

        double xCarre1 = lengths * lengths; 

        sommeXcarre += xCarre1; 

        //Calculates the average += echantillon 
        for (int k = 0; k < echantillon[i].length; k++) { 

         moyenneXbarre += echantillon[i][k]; 
        } 
       } 
            //That's where my average is calculated 
       moyenneXbarre /= lengths; 
      //Trying to calculated the standard deviation 
// Inside this ((sommeXbarre * sommeXbarre) I need the sum of numbers which Im not able to get 

      ecartType1 = Math.sqrt(
       (sommeXcarre - ((sommeXbarre * sommeXbarre)/echantillon.length)) 
       /echantillon.length 
      ); 

      ecartTypeXbarre = ecartType1/Math.sqrt(tailleEchantillon); 

      jtfMoyenneXBarre.setText(String.valueOf(moyenneXbarre)); 
      jtfEcartTypeXBarre.setText(String.valueOf(ecartTypeXbarre)); 
     } 
     catch (NumberFormatException e) { 

     } 
    } 
}); 
+0

考虑添加的信息作为代码注释。这太乱了,无法弄清楚。 – user2339071

+0

好吧,我现在编辑它。对不起 – Ramy

+0

“我不确定它是否实际采用了预先随机生成的值”,难道你不能使用调试器并找出结果吗? – 2013-11-05 05:57:55

回答

0

如果子阵列都是一样的大小,如果你宣布一个int[][] ray = new int[2][2];可以或多或少地做到这一点:

int[][] ray = { { 1, 2 }, { 3, 4 } }; 

// ray.length == 2 
// ray[0].length == 2 
// ray[1].length == 2 

double avg = 0; 

for (int i = 0; i < ray.length; i++) { 
    for (int k = 0; k < ray[i].length; k++) { 
     avg += ray[i][k]; 
    } 
} 

avg /= ray.length * ray[0].length; 

的问题是在Java中,他们不必是。你可以这样做:

int[][] ray = { { 1 }, { 2, 3, 4 } }; 

// ray.length == 2 
// ray[0].length == 1 
// ray[1].length == 3 

那么你真的应该总结的长度也:

double avg = 0; 
int lengths = 0; 

// loop for ray.length 
// ray.length == number of arrays in ray 
for (int i = 0; i < ray.length; i++) { 
    lengths += ray[i].length; 

    // loop for ray[i].length 
    // ray[i].length == number of elements in ray[i] 
    for (int k = 0; k < ray[i].length; k++) { 
     avg += ray[i][k]; 
    } 
} 

avg /= lengths; 
+0

我不确定我是否跟随你。我正在做的是我正在计算我认为之前生成的数字的总和,然后在这个下面// //计算刚刚高于这个值的生成值的总和(int i = 0; i Ramy

+0

我看到了尝试的平均值,但我不太确定你的循环是在做你想要的。现在它使用基于echantillon(echantillon.length)中包含的数组数的索引在echantillon [0]中遍历数组。如果二维数组中的数组长度与数组数量相等,则不会出现错误,但逻辑仍然不稳定。这就是我的第二个例子所说明的。 Java多维数组可以包含不同长度的数组。 – Radiodef

+0

基本上我想要它做的是把我产生的数字,然后把他们的总和,所以我可以得到平均水平,但如果我明白你的意思。你说我需要两个数值的长度,我之前输入以获得生成的数字? – Ramy