swapColumns method

void swapColumns(
  1. int col1,
  2. int col2
)

Swaps the position of two columns in the current matrix.

col1: The index of the first column to be swapped. col2: The index of the second column to be swapped.

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

Example:

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

Implementation

void swapColumns(int col1, int col2) {
  if (col1 < 0 || col1 >= columnCount || col2 < 0 || col2 >= columnCount) {
    throw Exception('Column indices are out of range');
  }

  for (int i = 0; i < rowCount; i++) {
    dynamic tempValue = _data[i][col1];
    _data[i][col1] = _data[i][col2];
    _data[i][col2] = tempValue;
  }
}