replicateMatrix method

Matrix replicateMatrix(
  1. int numRows,
  2. int numCols
)

Replicates the current matrix to create a new matrix with the desired dimensions.

The function copies the elements of the current matrix to fill the new matrix of size numRows x numCols. The new matrix is created by repeating the input matrix along the rows and columns.

numRows The desired number of rows in the output matrix. numCols The desired number of columns in the output matrix.

Returns a new matrix of size numRows x numCols with the replicated elements of the current matrix.

Example:

Matrix a = Matrix([
  [1, 2],
  [3, 4],
]);

Matrix replicated = a.replicateMatrix(4, 6);
print(replicated);

Output:

Matrix: 4x6
┌ 1  2  1  2  1  2 ┐
│ 3  4  3  4  3  4 │
│ 1  2  1  2  1  2 │
└ 3  4  3  4  3  4 ┘

Implementation

Matrix replicateMatrix(int numRows, int numCols) {
  // Create a new matrix with the desired dimensions
  Matrix result = Matrix.zeros(numRows, numCols);

  // Replicate the elements of the input matrix along the required dimensions
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      result[i][j] = this[i % rowCount][j % columnCount];
    }
  }

  return result;
}