diagonal method

List<List<double>> diagonal (List<List<double>> arr)

Passing in an m x n array, returns an array with only the main diagonals, all the rest are zeros

Implementation

List<List<double>> diagonal(List<List<double>> arr) {
  int nrows = arr.length;
  int ncols = arr[0].length;
  List<List<double>> out;
  out = numeric.createMatrix(nrows, ncols, 0.0);

  for (int i = 0; i < nrows; i++) {
    for (int j = 0; j < ncols; j++) {
      if (i == j) {
        out[i][j] = arr[i][j];
      }
    }
  }
  return out;
}