/ Published in: Java
You have to use float or double for the returned value, since the scale occurs in a continuous function.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; }