calculate Dispersion - Java java.lang

Java examples for java.lang:Math Algorithm

Description

calculate Dispersion

Demo Code


//package com.java2s;

public class Main {
    public static double calculateDispersion(int[][] data, int startx,
            int starty, int endx, int endy) {
        int elementsCount = (endx - startx) * (endy - starty);
        // counting average
        double average = 0;
        for (int row = startx; row < endx; ++row) {
            for (int col = starty; col < endy; ++col) {
                average += data[row][col];
            }//  w ww. j  av a 2  s .c om
        }
        average /= elementsCount;

        // counting summ of square difference
        double summ = 0;
        for (int row = startx; row < endx; ++row) {
            for (int col = starty; col < endy; ++col) {
                summ += (data[row][col] - average)
                        * (data[row][col] - average);
            }
        }

        return summ / elementsCount;
    }
}

Related Tutorials