Java tutorial
//package com.java2s; /* * This file is part of Skiggle, an online handwriting recognition * Java application. * Copyright (C) 2009-2011 Willie Lim <wlim650@gmail.com> * * Skiggle is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Skiggle 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 Skiggle. If not, see <http://www.gnu.org/licenses/>. * */ public class Main { public static int[] histogram(float[] dataPoints) { int[] buckets = { 0, 0, 0, 0, 0 }; // 5 buckets float minVal = 1000000.0F; float maxVal = -minVal; int numOfDataPoints = dataPoints.length; // Get the min and max values of the data points for (int i = 0; i < numOfDataPoints; i++) { minVal = Math.min(minVal, Math.abs(dataPoints[i])); maxVal = Math.max(maxVal, Math.abs(dataPoints[i])); } float bucketSize = (maxVal - minVal) / 5; // float bucketSize = 0.002F; // Count the number of points for each bucket for (int i = 0; i < numOfDataPoints; i++) { float val = Math.abs(dataPoints[i]); if (val <= minVal + bucketSize) buckets[0] = buckets[0] + 1; // between minVal and minVal + bucketSize else if (val <= minVal + 2 * bucketSize) buckets[1] = buckets[1] + 1; // between minVal and minVal + 2* bucketSize else if (val <= minVal + 3 * bucketSize) buckets[2] = buckets[2] + 1; // between minVal and minVal + 3* bucketSize else if (val <= minVal + 4 * bucketSize) buckets[3] = buckets[3] + 1; // between minVal and minVal + 4* bucketSize else buckets[4] = buckets[4] + 1; // greater than minVal + 4* bucketSize } return buckets; } }