median method

num median()

Returns the median of all elements in the matrix.

If the number of elements in the matrix is even, it will return the average of the two middle elements. If the number is odd, it will return the middle element.

Example:

var matrix = Matrix([[1.0, 2.0], [3.0, 4.0]]);
print(matrix.median()); // Output: 2.5

Implementation

num median() {
  var sortedData = _data.expand((element) => element).toList()..sort();
  int midIndex = sortedData.length ~/ 2;
  if (sortedData.length.isEven) {
    return (sortedData[midIndex - 1] + sortedData[midIndex]) / 2;
  } else {
    return sortedData[midIndex];
  }
}