Matrix.fromColumns constructor

Matrix.fromColumns(
  1. List<ColumnMatrix> columns, {
  2. bool resize = false,
})

Constructs a Matrix from a list of Column vectors.

All columns must have the same number of rows. Otherwise, an exception will be thrown.

Example:

var col1 = Column([1, 2, 3]);
var col2 = Column([4, 5, 6]);
var col3 = Column([7, 8, 9]);
var matrix = Matrix.fromColumns([col1, col2, col3]);
print(matrix);

Output:

1 4 7
2 5 8
3 6 9

Implementation

factory Matrix.fromColumns(List<ColumnMatrix> columns,
    {bool resize = false}) {
  final numRows = columns[0].rowCount;
  for (ColumnMatrix col in columns) {
    if (col.rowCount != numRows) {
      throw Exception('All columns must have the same number of rows');
    }
  }

  List<List<dynamic>> data =
      List.generate(numRows, (i) => List.filled(columns.length, 0));
  for (int j = 0; j < columns.length; j++) {
    for (int i = 0; i < numRows; i++) {
      data[i][j] = columns[j].getValueAt(i);
    }
  }
  return Matrix(data);
}