Java Array Normalize normalizeVectors(float[][] vectors, boolean maxMin)

Here you can find the source of normalizeVectors(float[][] vectors, boolean maxMin)

Description

normalize Vectors

License

Open Source License

Declaration

public static float[][] normalizeVectors(float[][] vectors,
            boolean maxMin) 

Method Source Code

//package com.java2s;
/*/*from   w  ww .  j  a va  2s . c  o m*/
 * Copyright (c) 2008-2013 Maksim Khadkevich and Fondazione Bruno Kessler.
 *
 * This file is part of MART.
 * MART is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2, as published
 * by the Free Software Foundation.
 *
 * MART is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with MART; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    public static float[][] normalizeVectors(float[][] vectors,
            boolean maxMin) {
        float[][] out = new float[vectors.length][];
        for (int i = 0; i < vectors.length; i++) {
            if (!maxMin) {
                out[i] = normalizeVector(vectors[i]);
            } else {
                out[i] = normalizeVectorMaxMin(vectors[i]);
            }
        }
        return out;
    }

    /**
     * Normalizes vector so that all values are in the range [0, 1]
     *
     * @param samples
     * @return
     */
    public static float[] normalizeVector(float[] samples) {
        float[] out = new float[samples.length];
        float maxValue = findMaxAbs(samples);
        if (maxValue != 0) {
            for (int i = 0; i < samples.length; i++) {
                out[i] = samples[i] / Math.abs(maxValue);
            }
        }
        return out;
    }

    /**
     * Normalizes vector so that all values are in the range [0, 1]
     *
     * @param samples
     * @return
     */
    public static float[] normalizeVectorMaxMin(float[] samples) {
        float[] out = new float[samples.length];
        float maxValue = findMax(samples);
        float minValue = findMin(samples);
        if (maxValue > minValue) {
            for (int i = 0; i < samples.length; i++) {
                out[i] = (samples[i] - minValue) / (maxValue - minValue);
            }
        } else {
            for (int i = 0; i < samples.length; i++) {
                out[i] = 0;
            }
        }
        return out;
    }

    /**
     * Finds maximal abs element in array
     *
     * @param array array
     * @return Minimum Value
     */
    public static float findMaxAbs(float[] array) {
        float max = 0;
        for (int i = 0; i < array.length; i++) {
            if (Math.abs(array[i]) > max) {
                max = array[i];
            }
        }
        return max;
    }

    /**
     * Finds maximal element in array
     *
     * @param array array
     * @return Minimum Value
     */
    public static float findMax(float[] array) {
        float max = Float.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    public static int findMax(int[] array) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    /**
     * Finds minimal element in array
     *
     * @param array array
     * @return Minimum Value
     */
    public static float findMin(float[] array) {
        float min = Float.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
}

Related

  1. normalizeVector(Double[] vector)
  2. normalizeVector(final double[] v)
  3. normalizeVector(float[] samples)
  4. normalizeVectorMax(double[] input)
  5. normalizeVectorMaxMin(float[] samples)
  6. normalizeVoxelDimensions(final double[] voxelDimensions)
  7. normalizeWith(double[] arr, double v)
  8. normalizeZscore(double[] x)
  9. normBySortedPointersInverse(double[] d, int[] pointers)