isSubMatrix method

bool isSubMatrix(
  1. Matrix parent
)

Checks if the current matrix is a submatrix of parent.

parent: The matrix in which to search for the current matrix.

Returns true if the current matrix is a submatrix of parent, otherwise false.

Example:

var parentMatrix = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
var subMatrix = Matrix([[5, 6], [8, 9]]);
var result = subMatrix.isSubMatrix(parentMatrix);
print(result); // Output: true

Implementation

bool isSubMatrix(Matrix parent) {
  for (int i = 0; i <= parent.rowCount - rowCount; i++) {
    for (int j = 0; j <= parent.columnCount - columnCount; j++) {
      bool found = true;
      for (int k = 0; k < rowCount && found; k++) {
        for (int l = 0; l < columnCount && found; l++) {
          if (parent[i + k][j + l] != this[k][l]) {
            found = false;
          }
        }
      }
      if (found) {
        return true;
      }
    }
  }
  return false;
}