Return to Snippet

Revision: 58945
at August 10, 2012 11:35 by rtperson


Initial Code
public double[] scaleValues(double[] vals) {
        double[] result = new double[vals.length];
        double min = minArray(vals);
        double max = maxArray(vals);
        double scaleFactor = max - min;
        // scaling between [0..1] for starters. Will generalize later.
        for (int x = 0; x < vals.length; x++) {
            result[x] = ((vals[x] - min) / scaleFactor);
        }
        return result;
    }

    // The standard collection classes don't have array min and max.
    public double minArray(double[] vals) {
        double min = vals[0];
        for (int x = 1; x < vals.length; x++) {
            if (vals[x] < min) {
                min = vals[x];
            }
        }
        return min;
    }

    public double maxArray(double[] vals) {
        double max = vals[0];
        for (int x = 1; x < vals.length; x++) {
            if (vals[x] > max) {
                max = vals[x];
            }
        }
        return max;
    }

Initial URL

                                

Initial Description
You have to use float or double for the returned value, since the scale occurs in a continuous function.

Initial Title
Scaling Values into a range between 0 and 1

Initial Tags
math

Initial Language
Java