commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ajo Fod <ajo....@gmail.com>
Subject Re: [Math] Could StatUtils use a method to find the index of the maximum value.
Date Sat, 21 Sep 2013 17:47:55 GMT
Here is a bit of code that ranks elements of a double array:

public class Rank {

    public static enum Dir {

        Ascending(1),
        Descending(-1);
        int side;

        Dir(int i) {
            side = i;
        }
    }

    /**
     * Rank elements of an array.
     *
     * @param dir : direction to rank elements in.
     * @param data : elements of the array.
     * @return
     */
    public static Integer[] rank(final Dir dir, final double[] data) {
        final Integer[] idx = new Integer[data.length];
        for (int i = 0; i < data.length; i++) {
            idx[i] = i;
        }

        Arrays.sort(idx, new Comparator<Integer>() {
            @Override
            public int compare(final Integer o1, final Integer o2) {
                return Double.compare(dir.side * data[o1], dir.side *
data[o2]);
            }
        });
        return idx;
    }

    public static void main(String[] args) {
        final double[] data = {1.7f, -0.3f, 2.1f, 0.5f};
        final Integer[] idx = rank(Dir.Descending, data);
        System.out.println(Arrays.toString(idx));
    }
}



On Thu, Sep 19, 2013 at 6:14 PM, Ajo Fod <ajo.fod@gmail.com> wrote:

> It currently has the maximum of a double array.
>
> -Ajo.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message