Create two arrays duplicate code
compare the difference and extract the differences into parameters.
you can look at this.

function createMatrix(domId) {
    var i, j;
    var htmlElements = "";
    var matrixWidth = parseInt(document.getElementById(domId).value, 10);
    var matrixHeight = parseInt(document.getElementById(domId).value, 10);

    if (matrixWidth &lt;= 7 &amp;&amp; matrixHeight &lt;= 7) {
        matrix = new Array(matrixHeight);
        for (i = 0; i &lt; matrix.length; i++) {
            matrix[i] = new Array(matrixWidth);
        }
        for (i = 0; i &lt; matrix.length; i++) {
            htmlElements += "&lt;div&gt;";
            for (j = 0; j &lt; matrix[i].length; j++) {
                matrix[i][j] = "&lt;div class=\"input-kontener\"&gt;&lt;input type=\"number\" class=\"form-control\" id=\"b" + (i + 1) + (j + 1) + "\" placeholder=\"b" + (i + 1) + (j + 1) + "\" &gt;&lt;/div&gt;";
                htmlElements += matrix[i][j];
            }
            htmlElements += "&lt;/div&gt;";
        }
        document.getElementById(domId).innerHTML = htmlElements;
    } else {
        document.getElementById(domId).innerHTML = "&lt;p&gt;Maximum matrix size is 7x7.&lt;/p&gt;";
    }
}

createMatrix('matrix-width-a');
createMatrix('matrix-width-b');