transpose2D function

List<List<double>> transpose2D(
  1. List<List<double>> a
)

Transposes a 2D list (swaps rows and columns).

Implementation

List<List<double>> transpose2D(List<List<double>> a) {
  if (a.isEmpty) return <List<double>>[];
  final int rows = a.length, cols = a[0].length;
  final List<List<double>> out = List.generate(
    cols,
    (_) => List<double>.filled(rows, 0.0),
  );
  for (int r = 0; r < rows; r++) {
    final List<double> row = a[r];
    for (int c = 0; c < cols; c++) {
      out[c][r] = row[c];
    }
  }
  return out;
}