Java Median median(float[] vector)

Here you can find the source of median(float[] vector)

Description

Calculates the median for a given vector (float array)

License

Open Source License

Parameter

Parameter Description
vector a parameter

Return

median

Declaration

public static float median(float[] vector) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Caleydo - Visualization for Molecular Biology - http://caleydo.org
 * Copyright (c) The Caleydo Team. All rights reserved.
 * Licensed under the new BSD license, available at http://caleydo.org/license
 ******************************************************************************/

import java.util.Arrays;

public class Main {
    /**// w  ww .  j a v  a  2  s . c o m
     * Calculates the median for a given vector (float array)
     *
     * @param vector
     * @return median
     */
    public static float median(float[] vector) {
        float median = 0;
        float[] temp = new float[vector.length];

        for (int i = 0; i < temp.length; i++) {

            if (Float.isNaN(vector[i]))
                temp[i] = 0;
            else
                temp[i] = vector[i];
        }

        Arrays.sort(temp);

        if ((temp.length % 2) == 0)
            median = (temp[(int) Math.floor(temp.length / 2)] + temp[(int) Math.floor((temp.length + 1) / 2)]) / 2;
        else
            median = temp[(int) Math.floor((temp.length + 1) / 2)];

        return median;
    }
}

Related

  1. median(final double[] values)
  2. median(final int[] values)
  3. median(float a, float b, float c)
  4. median(float[] vals)
  5. median(float[] values)
  6. median(int x[], int pos1, int pos2, int pos3)
  7. median(int[] m)
  8. median(int[] vals)
  9. median(Integer[] values)