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,938 questions

51,875 answers

573 users

How to multiply matrix in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[3, 3] { { 1, 8, 5 }, { 6, 7, 1 }, { 8, 7, 6 } };
            int[,] b = new int[3, 3] { { 4, 8, 1 }, { 6, 5, 3 }, { 4, 6, 5 } };
            int[,] c = new int[a.GetLength(0), b.GetLength(1)];

            Print(a);
            Console.WriteLine();
            Print(b);
            Console.WriteLine();

            // c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])

            for (int i = 0; i < a.GetLength(0); i++)
                for (int j = 0; j < a.GetLength(1); j++)
                    c[i, j] = Calc(a, b, i, j);
            Print(c);
        }
        static void Print(int[,] arr2d)
        {
            for (int i = 0; i < arr2d.GetLength(0); i++)
            {
                for (int j = 0; j < arr2d.GetLength(1); j++)
                    Console.Write("{0,4}", arr2d[i, j]);
                Console.WriteLine();
            }
        }
        static int Calc(int[,] a, int[,] b, int i, int j)
        {
            int sum = 0;
            for (int x = 0; x < a.GetLength(0); x++) 
                sum += a[i, x] * b[x, j];
            // Console.Write("a[{0}][{1}] * b[{2}][{3}] + ", i, x, x, j); 
            return sum;
        }
    }
}


/*
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 Jun 1, 2014 by avibootz
edited Feb 28, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[3, 3] { { 1, 8, 5 }, { 6, 7, 1 }, { 8, 7, 6 } };
            int[,] b = new int[3, 3] { { 4, 8, 1 }, { 6, 5, 3 }, { 4, 6, 5 } };
            int[,] c = new int[a.GetLength(0), b.GetLength(1)];

            Print(a);
            Console.WriteLine();
            Print(b);
            Console.WriteLine();

            // 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);

            Print(c);
        }
        static void Print(int[,] arr2d)
        {
            for (int i = 0; i < arr2d.GetLength(0); i++)
            {
                for (int j = 0; j < arr2d.GetLength(1); j++)
                    Console.Write("{0,4}", arr2d[i, j]);
                Console.WriteLine();
            }
        }
        static void multiple_matrix(int[,] a, int[,] b, int[,] c)
        {
            for (int i = 0; i < c.GetLength(0); i++)
            {
                for (int j = 0; j < c.GetLength(1); j++)
                {
                    c[i, j] = 0;
                    for (int k = 0; k < a.GetLength(1); k++) 
                        c[i, j] = c[i, j] + a[i, k] * b[k, j];
                }
            }
        }
    }
}


/*
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 28, 2016 by avibootz

Related questions

1 answer 130 views
1 answer 51 views
1 answer 49 views
1 answer 49 views
1 answer 51 views
51 views asked Oct 1, 2025 by avibootz
...