Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to multiply matrices in JavaScript

4 Answers

0 votes
<!DOCTYPE html>
<html>
<head></head>
<body>

<script type="text/javascript">
function Print(arr2d, size)
{
    document.write('<table border="0" cellspacing="3">');
    for (let i = 0; i < size; i++)
    {
        document.write("<tr align='right'>");
        for (let j = 0; j < size; j++)
            document.write("<td>" + arr2d[i][j] + "</td>");
        document.write("</tr>");
    }
    document.write("</table>");
}
function Calc(a, b, i, j, size)
{
    let sum = 0;
     
    for (let x = 0; x < size; x++) 
    {
        sum += a[i][x] * b[x][j];
    }

    return sum;
}      

const a = [[ 1, 8, 5 ], [ 6, 7, 1 ], [ 8, 7, 6 ]];
const b = [[ 4, 8, 1 ], [ 6, 5, 3 ], [ 4, 6, 5 ]];
const size = 3;

let c = new Array(size);
for (let i = 0; i < size; i++) 
  c[i] = new Array(size);

  
Print(a, size);
document.write("<br />");
Print(b, size);
document.write("<br />");
  
// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])
                      
for (let i = 0; i < size; i++)
    for (let j = 0; j < size; j++)
        c[i][j] = Calc(a, b, i, j, size);
        
Print(c, size);   
</script>

</body>
</html>

<!--
run: 

1    8    5
6    7    1
8    7    6

4    8    1
6    5    3
4    6    5

72     78    50
70     89    32
98    135    59

-->

 



answered Feb 29, 2016 by avibootz
edited Apr 29, 2024 by avibootz
0 votes
<!DOCTYPE html>
<html>
<head></head>
<body>

<script type="text/javascript">
function Print(arr2d, size)
{
    document.write('<table border="0" cellspacing="3">');
    for (let i = 0; i < size; i++)
    {
        document.write("<tr align='right'>");
        for (let j = 0; j < size; j++)
            document.write("<td>" + arr2d[i][j] + "</td>");
        document.write("</tr>");
    }
    document.write("</table>");
}
function multiple_matrix(a, b, c, size)
{
    for (let i = 0; i < size; i++)
    {
        for (let j = 0; j < size; j++)
        {
            c[i][j] = 0;
            for (let k = 0; k < size; k++) 
                c[i][j] = c[i][j] + a[i][k] * b[k][j];
        }
    }
}           

const a = [[ 1, 8, 5 ], [ 6, 7, 1 ], [ 8, 7, 6 ]];
const b = [[ 4, 8, 1 ], [ 6, 5, 3 ], [ 4, 6, 5 ]];
const size = 3;

let c = new Array(size);
for (let i = 0; i < size; i++) 
    c[i] = new Array(size);

  
Print(a, size);
document.write("<br />");
Print(b, size);
document.write("<br />");
  
// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])
                      
multiple_matrix(a, b, c, size);
        
Print(c, size);  
</script>

</body>
</html>

<!--
run: 

1    8    5
6    7    1
8    7    6

4    8    1
6    5    3
4    6    5

72     78    50
70     89    32
98    135    59

-->

 



answered Feb 29, 2016 by avibootz
edited Apr 29, 2024 by avibootz
0 votes
function Print(arr2d, size) {
    for (let i = 0; i < size; i++) {
        let s = "";
        for (let j = 0; j < size; j++)
            s +=  arr2d[i][j] + " ";
        console.log(s);
    }
    console.log();
}

function Calc(a, b, i, j, size) {
    let sum = 0;
      
    for (let x = 0; x < size; x++)  {
        sum += a[i][x] * b[x][j];
    }
 
    return sum;
}      
 
const a = [ [ 1, 8, 5 ], 
            [ 6, 7, 1 ], 
            [ 8, 7, 6 ]];
const b = [ [ 4, 8, 1 ], 
            [ 6, 5, 3 ], 
            [ 4, 6, 5 ]];
const size = 3;

let c = new Array(size);
for (let i = 0; i < size; i++) {
    c[i] = new Array(size);
}

Print(a, size);
Print(b, size);

// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])
// c[0, 0] = 1 * 4 + 8 * 6 + 5 * 4 = 4 + 48 + 20 = 72
                        
for (let i = 0; i < size; i++) {
    for (let j = 0; j < size; j++) {
        c[i][j] = Calc(a, b, i, j, size);
    }
}
         
Print(c, size);


 
/*
run:
   
1 8 5 
6 7 1 
8 7 6 

4 8 1 
6 5 3 
4 6 5 

72 78 50 
70 89 32 
98 135 59 
     
*/

 



answered Apr 29, 2024 by avibootz
0 votes
function Print(arr2d, size) {
    for (let i = 0; i < size; i++) {
        let s = "";
        for (let j = 0; j < size; j++)
            s +=  arr2d[i][j] + " ";
        console.log(s);
    }
    console.log();
}

function multiple_matrix(a, b, c, size) {
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            c[i][j] = 0;
            for (let k = 0; k < size; k++) {
                c[i][j] = c[i][j] + a[i][k] * b[k][j];
            }
        }
    }
}     
 
const a = [ [ 1, 8, 5 ], 
            [ 6, 7, 1 ], 
            [ 8, 7, 6 ]];
const b = [ [ 4, 8, 1 ], 
            [ 6, 5, 3 ], 
            [ 4, 6, 5 ]];
const size = 3;

let c = new Array(size);
for (let i = 0; i < size; i++) {
    c[i] = new Array(size);
}

Print(a, size);
Print(b, size);

// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])
// c[0, 0] = 1 * 4 + 8 * 6 + 5 * 4 = 4 + 48 + 20 = 72
                        
multiple_matrix(a, b, c, size);
         
Print(c, size);


 
/*
run:
   
1 8 5 
6 7 1 
8 7 6 

4 8 1 
6 5 3 
4 6 5 

72 78 50 
70 89 32 
98 135 59 
     
*/

 



answered Apr 29, 2024 by avibootz

Related questions

1 answer 101 views
1 answer 89 views
1 answer 92 views
92 views asked Mar 4, 2025 by avibootz
1 answer 69 views
1 answer 62 views
62 views asked Mar 4, 2025 by avibootz
1 answer 65 views
65 views asked Mar 4, 2025 by avibootz
1 answer 61 views
61 views asked Mar 4, 2025 by avibootz
...