multiply matrices

node v8.17.0
version: 1.0.0
endpointsharetweet
// A = [[1,2,3],[4,5,6],[7,8,9]] // b = [[1],[2],[3]] // m = Zeilen // n = Spalten // (m,x) * (x,n) = (m,n)
function mrxLen(A,B) { for(var m in A) { if(A[m].length != B.length) return; } return A.length; }
function vMkr(len,nB) { let v = []; for(var i = 0; i < len; i++) { v.push([]); for(var j = 0; j < nB; j++) v[i].push(0); } return v; }
function nLen(M) { for(var m in M) { if(M[m].length != M[0].length) return; } return M[m].length; };
function mvp(A,B) { let mmax = mrxLen(A,B); let nA = nLen(A); let nB = nLen(B); console.log("Zeilen v: " + mmax + ", Spalten v: " + nB); let v = vMkr(mmax,nB); console.log(v); for(var m in v) { for(var n in v[m]) { for(var i in A[m]) { console.log("Zeile v: " + m + ", Spalte v: " + n); console.log("Zeile A: " + m + ", Spalte A: " + i + ", Wert A: " + A[m][i] + ", Wert B: " + B[i][m]); v[m][n] = v[m][n] + A[m][i] * B[i][n]; } } } return v; }
mvp([[2,-1],[3,-2],[-1,2]],[[1,-2,-3],[-3,3,-2]]);
Loading…

no comments

    sign in to comment