Ask Your Question
0

Get the median, added to mean and std value

asked 2013-09-11 04:36:40 -0600

updated 2013-09-26 16:40:09 -0600

I'm using : Core.meanStdDev(hist, mean, stdev);

Is there a possibility to have median in the result of meanStdDev ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-23 06:28:49 -0600

I found a simple way to answer :

double getMedian(Mat hist) {
    // binapprox algorithm

    long n = hist.total();
    int[] histBuff = new int[(int) n];
    hist.get(0, 0, histBuff);
    // Compute the mean and standard deviation
    // int n = x.length;
    double sum = 0;
    // int i;
    for (int i = 0; i < n; i++) {
        sum += histBuff[i];
    }
    double mu = sum / n;

    sum = 0;
    for (int i = 0; i < n; i++) {
        sum += (histBuff[i] - mu) * (histBuff[i] - mu);
    }
    double sigma = Math.sqrt(sum / n);

    // Bin x across the interval [mu-sigma, mu+sigma]
    int bottomcount = 0;
    int[] bincounts = new int[1001];
    for (int i = 0; i < 1001; i++) {
        bincounts[i] = 0;
    }
    double scalefactor = 1000 / (2 * sigma);
    double leftend = mu - sigma;
    double rightend = mu + sigma;
    int bin;

    for (int i = 0; i < n; i++) {
        if (histBuff[i] < leftend) {
        bottomcount++;
        } else if (histBuff[i] < rightend) {
        bin = (int) ((histBuff[i] - leftend) * scalefactor);
        bincounts[bin]++;
        }
    }

    double median = 0;
    // If n is odd
    if ((n % 2) != 0) {
        // Find the bin that contains the median
        int k = (int) ((n + 1) / 2);
        int count = bottomcount;

        for (int i = 0; i < 1001; i++) {
        count += bincounts[i];

        if (count >= k) {
            median = (i + 0.5) / scalefactor + leftend;
        }
        }
    }

    // If n is even
    else {
        // Find the bins that contains the medians
        int k = (int) (n / 2);
        int count = bottomcount;

        for (int i = 0; i < 1001; i++) {
        count += bincounts[i];

        if (count >= k) {
            int j = i;
            while (count == k) {
            j++;
            count += bincounts[j];
            }
            median = (i + j + 1) / (2 * scalefactor) + leftend;
        }
        }
    }
    return median;
    }
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-09-11 04:36:40 -0600

Seen: 11,879 times

Last updated: Sep 26 '13