Java Array Normalize normalize(double[] values)

Here you can find the source of normalize(double[] values)

Description

Normalize to a distribution in which the sum of all the elements is 1

License

Apache License

Parameter

Parameter Description
values a parameter

Declaration

public static double[] normalize(double[] values) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

public class Main {
    /**//from  w w w  .  j a v a  2  s.c  om
     * Normalize to a distribution in which the sum of all the elements is 1
     *
     * @param values
     * @return
     */
    public static double[] normalize(double[] values) {
        double sum = sum(values);
        double[] norm = new double[values.length];
        for (int i = 0; i < norm.length; i++) {
            norm[i] = values[i] / sum;
        }
        return norm;
    }

    public static double sum(ArrayList<Double> values) {
        double sum = 0;
        for (double value : values) {
            sum += value;
        }
        return sum;
    }

    public static double sum(double[] values) {
        double sum = 0;
        for (double value : values) {
            sum += value;
        }
        return sum;
    }

    public static int sum(int[] values) {
        int sum = 0;
        for (int value : values) {
            sum += value;
        }
        return sum;
    }
}

Related

  1. normalize(double[] probDist)
  2. normalize(double[] state)
  3. normalize(double[] v)
  4. normalize(double[] v)
  5. normalize(double[] vals)
  6. normalize(double[] values)
  7. normalize(double[] vector)
  8. normalize(double[] vector)
  9. normalize(double[] w)