Java Array Normalize normalizeVector(float[] samples)

Here you can find the source of normalizeVector(float[] samples)

Description

Normalizes vector so that all values are in the range [0, 1]

License

Open Source License

Parameter

Parameter Description
samples a parameter

Declaration

public static float[] normalizeVector(float[] samples) 

Method Source Code

//package com.java2s;
/*//from   ww w  .  j  a  v  a 2 s .  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 {
    /**
     * 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;
    }

    /**
     * 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;
    }
}

Related

  1. normalizeTrimmedText(char[] ch)
  2. NormalizeVec2D(double[] vec)
  3. normalizeVector(double[] input)
  4. normalizeVector(Double[] vector)
  5. normalizeVector(final double[] v)
  6. normalizeVectorMax(double[] input)
  7. normalizeVectorMaxMin(float[] samples)
  8. normalizeVectors(float[][] vectors, boolean maxMin)
  9. normalizeVoxelDimensions(final double[] voxelDimensions)