median method

dynamic median()

Returns the median value of the vector. If the vector has an even number of elements, the median is calculated as the mean of the two central elements.

Usage:

var vector = Vector([1.0, 2.0, 3.0, 4.0, 5.0]);
print(vector.median());  // Output: 3.0

Implementation

dynamic median() {
  List sortedData = _data..sort();
  int midIndex = sortedData.length ~/ 2;
  if (sortedData.length.isEven) {
    return (sortedData[midIndex - 1] + sortedData[midIndex]) / 2;
  } else {
    return sortedData[midIndex];
  }
}