swapRows method

void swapRows(
  1. int row1,
  2. int row2
)

Swaps the position of two rows in the current matrix.

row1: The index of the first row to be swapped. row2: The index of the second row to be swapped.

Throws an exception if either row index is out of range.

Example:

var matrix = Matrix([[1, 2], [3, 4]]);
matrix.swapRows(0, 1);
print(matrix);
// Output:
// 3 4
// 1 2

Implementation

void swapRows(int row1, int row2) {
  if (row1 < 0 || row1 >= rowCount || row2 < 0 || row2 >= rowCount) {
    throw Exception('Row indices are out of range');
  }

  List<dynamic> tempRow = _data[row1];
  _data[row1] = _data[row2];
  _data[row2] = tempRow;
}