Answer the question
In order to leave comments, you need to log in
How to multiply two matrices?
There is a function for obtaining data from inputs and a function for obtaining a product from multiplication. That's it with the multiplication function I got confused. Tell me in 1 is the definition of matrices correct at all? why does it write that they are not protected when calling variables in the console? for example variable A or rows? Is the variable C or t the product of two matrices? and how, for example, to display the product in the console?
html here
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 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;
}
}
return C;
}
Answer the question
In order to leave comments, you need to log in
If I were you, I would not implement the MultiplyMatrix function, unless for some educational purposes, but would use numericjs
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question