Java Utililty Methods Array Normalize

List of utility methods to do Array Normalize

Description

The list of methods to do Array Normalize are organized into topic(s).

Method

double[]normalizeHistogram(int[] h)
normalize Histogram
int max = h[0];
for (int i = 0; i < h.length; i++) {
    if (h[i] > max)
        max = h[i];
if (max == 0)
    return null;
double[] hn = new double[h.length];
...
double[]normalizeHistogram(int[] hist)
normalizes histogram in hist to produce ratios of each position to total sum
int sum = 0;
for (int i : hist)
    sum += i;
return normalizeHistogramToBase(hist, sum);
double[]normalizeHistogramToBase(int[] hist, double base)
returns histogram counts divided by given base
double[] result = new double[hist.length];
for (int i = 0; i < hist.length; i++)
    result[i] = (base == 0) ? 0.0 : ((double) hist[i]) / base;
return result;
double[][]normalizeInnerPointDistanceMax(double[][] distances, double maxDist)
normalizes the inner point distances of the point in the distance matrix, such that die maximum inner-point distance is maxDist
double max = -1;
for (int i = 0; i < distances.length; ++i) {
    for (int j = i + 1; j < distances[i].length; ++j) {
        if (max < distances[i][j] || max == -1) {
            max = distances[i][j];
for (int i = 0; i < distances.length; ++i) {
    for (int j = 0; j < distances[i].length; ++j) {
        distances[i][j] = distances[i][j] / max * maxDist;
return distances;
voidnormalizeL2(double[] a)
L2 normalization
double sum = 0;
for (double x : a)
    sum += x * x;
sum = Math.sqrt(sum);
if (sum == 0)
    for (int i = 0; i < a.length; i++)
        a[i] = 1 / a.length;
else
...
double[]normalizeL2(double[] histogram)
Euclidean normalization of a double[] histogram.
double[] result = new double[histogram.length];
double len = 0;
for (int i = 0; i < histogram.length; i++) {
    len += histogram[i] * histogram[i];
len = Math.sqrt(len);
for (int i = 0; i < histogram.length; i++) {
    if (histogram[i] != 0)
...
voidnormalizeLengthInPlace(double[] in)
normalize Length In Place
double sum = (Math.sqrt(sumSquares(in)));
for (int i = 0; i < in.length; i++) {
    in[i] = in[i] / sum;
voidnormalizeLog(double[] logs)
normalize Log
double max = max(logs);
double norm = 0.0;
for (int i = 0; i < logs.length; i++) {
    norm += Math.exp(logs[i] - max);
norm = Math.log(norm) + max;
add(logs, -norm);
exp(logs);
...
voidnormalizeMatrix(double[][] M)
Adjusts matrix elements so that sum of all positive elements in each row does not exceed 1.
if (M == null)
    return;
int m = M.length;
int n = M[0].length;
for (int i = 0; i < m; i++) {
    boolean hasNegative = false;
    int countNegative = 0;
    int idxNegative = -1;
...
voidnormalizeMatrix(float[] cm)
Normalizes the matrix to values to [0..1].
float max = 0.0f;
for (int i = 0; i < cm.length; i++) {
    if (max < cm[i]) {
        max = cm[i];
if (max <= 0.0) {
    return;
...