View Full Version : 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

Mishu

03-28-2012, 03:55 AM

[2 4] * [3 8] = [14 20]

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.

Old Pedant

03-28-2012, 04:02 AM

http://www.mathsisfun.com/algebra/matrix-multiplying.html

His two matrices are actually

| 2 4 |

| 3 1 |

and

| 3 8 |

| 2 1 |

each is a 2x2 matrix.

Mishu

03-28-2012, 04:07 AM

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.

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]

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]

ok ok thanks i will give you my try and you could give some hints in what can i do to improve the program

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 < y-1; j++) {

alert(" "+array1[i][j]);

}

alert();

}

for( i = 0; i < x; i++) {

for( j = 0; j < y-1; 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 < y-1; j++) {

alert(" " +array2[i][j]);

}

alert();

}

}

}

Old Pedant

03-28-2012, 06:39 AM

Please, when you post code, wrap it in [ code ] ... [ /code ] tags (without spaces in them) and indent the code.

Old Pedant

03-28-2012, 07:33 AM

Here. Using document.write, so it's not pretty. But it demonstrates the process thoroughly, I hope.

I haven't tested it with non-square 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.

<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>

Old Pedant

03-28-2012, 07:36 AM

Ahhh...one trivial change. In red, below. Same as the example given on the aforementioned link.

<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>

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.