Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How do i multiply two matices in javascript

1. ## 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

• Originally Posted by Sler
[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.

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

• 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

for(i = 0; i < x; i++) {
for(j = 0; j <= x; j++) {
}
}
var y= array1.length;
for( i = 0; i < y; i++) {
for( j = 0; j < y-1; j++) {
}
}

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++) {
}
}
}
}

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

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

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("&nbsp;&nbsp;[" + ans[arow].join(",") + "]" );
}
line("]");

</script>

• 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("&nbsp;&nbsp;[" + ans[arow].join(",") + "]" );
}
line("]");

</script>

•