public static Matrix[] sampleWithReplacement(Matrix features, Matrix labels, int n) {
        if (features == null || labels == null || features.getNumRows() != labels.getNumRows()) {
            throw new MLException("No training data or size mismatch.");
        }
        Matrix matrices[] = new Matrix[] { new Matrix(features, true),
                new Matrix(labels, true) };

        int size = features.getNumRows();
        while (matrices[0].getNumRows() != n) {
            int rand = nextInt(size);
            matrices[0].addRow(features.getRow(rand));
            matrices[1].addRow(labels.getRow(rand));
        }
        return matrices;
    }