Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 11 of 11

03282012, 02:37 AM #1
 Join Date
 Mar 2012
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
How do i multiply two matices in javascript
Hi im new in javascript and im having some problems with the two dimensional arrays problems.
Well basically what i need to do in this program its to multiply two matrices of the same length and print the result as a matrix
Example
[2 4] * [3 8] = [14 20]
[3 2] [2 1] [13 26]
I hope someone could help me im really stuck in this problem

03282012, 02:55 AM #2
 Join Date
 Mar 2012
 Posts
 306
 Thanks
 1
 Thanked 28 Times in 28 Posts
It's been a while since I multiplied matrices in high school math. If you had to do it manually, what is the process in getting
[14 20]
by multiplying[2 4] * [3 8]
?
When you have the manual process it should be fairly easy to write up the javascript to do the calculations.
Can you post the manual proces which will be effectively the pseudo code for the task.

03282012, 03:02 AM #3
http://www.mathsisfun.com/algebra/ma...ltiplying.html
His two matrices are actually
Code: 2 4   3 1  and  3 8   2 1  each is a 2x2 matrix.
Be yourself. No one else is as qualified.

03282012, 03:07 AM #4
 Join Date
 Mar 2012
 Posts
 306
 Thanks
 1
 Thanked 28 Times in 28 Posts
ok then that makes it fairly easy. But I think this is homework so I'll wait to see if sler posts their attempt first.
1.5) No homework assignments  Do not post your entire homework assignment and request that other members do it for you. This is considered cheating, and your thread may even be used by your school to prove your guilt. Now, you may ask for advice or help on a specific aspect of your assignment that you're having trouble with. Use common sense as far as what's acceptable in terms of soliciting help with homework assignments.Last edited by Mishu; 03282012 at 03:14 AM.

03282012, 03:13 AM #5
 Join Date
 Mar 2012
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
Yea sure no problem, but i think you got confused a little bit with the multiplication. In the example im multiplying two matrices with two rows and two columns so the process will look like this:
[2 4] * [3 8] = [2*3+4*2=14 2*8+4*1 = 20]
[3 2] [2 1] [3*3+2*2=13 3*8+2*1 = 26]
[2 4] * [3 8] = [14 20]
[3 2] [2 1] [13 26]

03282012, 03:17 AM #6
 Join Date
 Mar 2012
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
I included double asterisk so it looks better but it this only represent as multiplying this matrix[2 4] times this one [3 8]
[3 2] [2 1]
[2 4] * [3 8] = [2*3+4*2=14 2*8+4*1 = 20]
[3 2] * [2 1] [3*3+2*2=13 3*8+2*1 = 26]
[2 4] * [3 8] = [14 20]
[3 2] * [2 1] [13 26]

03282012, 03:39 AM #7
 Join Date
 Mar 2012
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
ok ok thanks i will give you my try and you could give some hints in what can i do to improve the program

03282012, 05:13 AM #8
 Join Date
 Mar 2012
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
So i received some help from my brother but he uses C so i tried my best to write the program in javascript. I think that one of the cycles in the matrices is wrong cause i keep getting as undefined the last two values in each matrix. I thought it would work cause i get the first values correct. This is my code:
var array=[[2,8],[3,1]] ;
var array1=[[3,4],[4,5]] ;
var array2=[[],[]];
var x= array.length;
var y=array1.length;
var j=0,k=0,i=0
alert("Matrix 1 : ");
for(i = 0; i < x; i++) {
for(j = 0; j <= x; j++) {
alert(" "+ array[i][j]);
}
alert();
}
var y= array1.length;
alert("Matrix 2 : ");
for( i = 0; i < y; i++) {
for( j = 0; j < y1; j++) {
alert(" "+array1[i][j]);
}
alert();
}
for( i = 0; i < x; i++) {
for( j = 0; j < y1; j++) {
for( k = 0; k < y; k++){
array2[i][j] += array[i][k]*array1[k][j];
}
}
}
alert("Multiply of both matrix : ");
for( i = 0; i < x; i++) {
for( j = 0; j < y1; j++) {
alert(" " +array2[i][j]);
}
alert();
}
}
}

03282012, 05:39 AM #9
Please, when you post code, wrap it in [ code ] ... [ /code ] tags (without spaces in them) and indent the code.
Be yourself. No one else is as qualified.

03282012, 06:33 AM #10
Here. Using document.write, so it's not pretty. But it demonstrates the process thoroughly, I hope.
I haven't tested it with nonsquare matrices, hence the "KABLOOEY".
Should work if I get height and width in the right places, which I don't think they are in this code.
Code:<script> function line(txt) { if(txt==null) txt = ""; document.write( txt + "<br/>\n" ); } var m1 = [ [2,4], [3,2] ] ; var m2 = [ [3,8], [2,1] ] ; var ans = [ [], [] ]; var height = m1.length; var width = m1[0].length; if ( height != width ) alert("KABLOOEY"); for ( var m1row = 0; m1row < height; ++m1row ) { for ( var m2col = 0; m2col < width; ++m2col ) { var sum = 0; for ( var m1col = 0; m1col < width; ++m1col ) { line( m1[m1row][m1col] + " X " + m2[m1col][m2col] ); sum += m1[m1row][m1col] * m2[m1col][m2col]; } ans[m1row][m2col] = sum; line( "SUM: " + sum); } } line("["); for ( var arow = 0; arow < ans.length; ++arow ) { line(" [" + ans[arow].join(",") + "]" ); } line("]"); </script>
Be yourself. No one else is as qualified.

03282012, 06:36 AM #11
Ahhh...one trivial change. In red, below. Same as the example given on the aforementioned link.
Code:<script> function line(txt) { if(txt==null) txt = ""; document.write( txt + "<br/>\n" ); } var m1 = [ [1,2,3], [4,5,6] ] ; var m2 = [ [7,8], [9,10], [11,12] ] ; var ans = [ [], [] ]; var height = m1.length; var width = m1[0].length; // height/width of 2nd matrix must match width/height of first one: if ( m2.length != width ) alert("KABLOOEY"); if ( m2[0].length != height ) alert("KABLOOEY") for ( var m1row = 0; m1row < height; ++m1row ) { for ( var m2col = 0; m2col < height; ++m2col ) { var sum = 0; for ( var m1col = 0; m1col < width; ++m1col ) { line( m1[m1row][m1col] + " X " + m2[m1col][m2col] ); sum += m1[m1row][m1col] * m2[m1col][m2col]; } ans[m1row][m2col] = sum; line( "SUM: " + sum); } } line("["); for ( var arow = 0; arow < ans.length; ++arow ) { line(" [" + ans[arow].join(",") + "]" ); } line("]"); </script>
Last edited by Old Pedant; 03282012 at 06:41 AM.
Be yourself. No one else is as qualified.