appendRows method

Matrix appendRows(
  1. dynamic newRows
)

Appends new rows to the matrix with the values provided in newRows.

newRows: A list of rows or a Matrix to append to the matrix

Returns a new matrix with the appended rows

Example:

var matrix = Matrix.fromList([[1, 2], [3, 4]]);
matrix = matrix.appendRows(Matrix.fromList([[5, 6], [7, 8]]));
print(matrix);
// Output:
// Matrix: 4x2
// ┌ 1 2 ┐
// | 3 4 |
// | 5 6 |
// └ 7 8 ┘

Implementation

Matrix appendRows(dynamic newRows) {
  List<List<dynamic>> rowsToAdd;
  if (newRows is Matrix) {
    rowsToAdd = newRows.toList();
  } else if (newRows is List<List<dynamic>>) {
    rowsToAdd = newRows;
  } else {
    throw Exception('Invalid input type');
  }

  if (columnCount != rowsToAdd[0].length) {
    throw Exception(
        'The new rows must have the same number of columns as the matrix');
  }
  var newData = List<List<dynamic>>.from(_data);
  newData.addAll(rowsToAdd);
  return Matrix(newData);
}