There are 3 matrices. The output of the matrices A and B should be output into the matrix C. The question is how to divide the data of the array and output each value separately in the input? I understand you need a cycle in which there will be a separation of values ​​and output to the required place. While it is worth outputting the work only to the console.

I understand the easiest way to decompose an array is through foreach? But it doesn’t seem to work in ie8.

Below, the first function reads the values ​​from the inputs of the matrices A and B and, even lower, the function that multiplies them. http://codepen.io/anon/pen/vGPEde full code.

function readMatrixFromDom(aClassName) { var result = []; var rows = $('.' + aClassName).find('tr'); for (var i = 0; i < rows.length; i++) { result.push([]); var cells = $(rows[i]).find('td > input'); for (var j = 0; j < cells.length; j++) { result[i].push(+$(cells[j]).val()); } } return result; } function MultiplyMatrix(A,B){ var A = readMatrixFromDom('matrix_a'); var B = readMatrixFromDom('matrix_b'); var c_mat = readMatrixFromDom('matrix_c'); var rowsA = A.length, colsA = A[0].length, rowsB = B.length, colsB = B[0].length, C = []; if (colsA != rowsB) return false; for (var i = 0; i < rowsA; i++) C[i] = []; for (var k = 0; k < colsB; k++){ for (var i = 0; i < rowsA; i++){ var t = 0; for (var j = 0; j < rowsB; j++) t += A[i][j]*B[j][k]; C[i][k] = t; } } console.log(C); }); return C; } $(document).on('click', '.umn' , function () { MultiplyMatrix(); }); 

    1 answer 1

    So, we will continue (I pay tribute to your perseverance :)).

     function writeMatrixToDom(aMatrix, aClassName) { console.log("Result matrix: "); console.log(aMatrix); console.log("Result class: " + aClassName); var rows = $('.' + aClassName).find('tr'); var rowCount = Math.min(rows.length, aMatrix.length); for (var i = 0; i < rowCount; i++) { var cells = $(rows[i]).find('td > input'); var cellCount = Math.min(cells.length, aMatrix[i].length); for (var j = 0; j < cellCount; j++) { $(cells[j]).val(aMatrix[i][j]); } } } function MultiplyMatrix(A, B) { var rowsA = A.length, colsA = A[0].length, rowsB = B.length, colsB = B[0].length, C = []; if (colsA != rowsB) return C; for (var i = 0; i < rowsA; i++) C[i] = []; for (var k = 0; k < colsB; k++) { for (var i = 0; i < rowsA; i++) { var t = 0; for (var j = 0; j < rowsB; j++) t += A[i][j] * B[j][k]; C[i][k] = t; } } console.log(C); return C; } $(document).on('click', '.umn' , function () { var A = readMatrixFromDom('matrix_a'); var B = readMatrixFromDom('matrix_b'); // проверка размерностей A и B на возможность их перемножения - добавить var C = MultiplyMatrix(A, B); // проверка размерности таблицы в matrix_c на соответствие размерности C - добавить writeMatrixToDom(C, 'matrix_c'); }); 
    • T. e all the same foreach is not the best option? - Drop
    • @Drop I see no fundamental difference in the way in which you go through the elements of an array. Just with for you can pre-calculate the upper bound, including the restriction on the number of input elements. - Igor
    • With each of your answers, I’ve learned more and more js) I’m not sure yet in the slope I swear at the line var cellCount = Math.min (cells.length. AMatrix [i] .length); Like "Cannot read property '0' of undefined" - Drop
    • @Drop - sorry - there is, of course, a comma, not a dot: cells.length, - Igor
    • By the way, I noticed that the multiplication function did not work when the number of columns of the matrix A is not equal to the number of rows of the matrix B. Now the magic happens and everything is fucking output to the C matrix. .length); Cannot read property 'length' of null - Drop