setSubMatrix method

void setSubMatrix(
  1. int startRow,
  2. int startCol,
  3. Matrix subMatrix
)

Sets the values of the subMatrix at the specified position.

The parameters startRow and startCol specify the starting row and column indices, and subMatrix is the subMatrix to be inserted.

Example:

Matrix mat = Matrix.fromList([
  [4, 5, 6, 7],
  [9, 9, 8, 6],
  [1, 1, 2, 9]
]);

Matrix subMat = Matrix.fromList([
  [3, 3],
  [3, 3]
]);

mat.setSubMatrix(1, 1, subMat);
print(mat);

Output:
Matrix: 3x4
┌ 4 5 6 7 ┐
│ 9 3 3 6 │
└ 1 3 3 9 ┘

Implementation

void setSubMatrix(int startRow, int startCol, Matrix subMatrix) {
  int rows = subMatrix.rowCount;
  int cols = subMatrix.columnCount;
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      _data[startRow + i][startCol + j] = subMatrix[i][j];
    }
  }
}