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) {

        }
    }
});
4

1 に答える 1

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;
于 2013-11-05T06:26:48.590 に答える