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 two matrices (matrix) in C#

1 Answer

0 votes
using System;

public class Program {
	public const int COLS1 = 3;
	public const int COLS2 = 2;

	public static void Main(string[] args)
	{
		int[][] matrix1 = new int[][] {
			new int[] {4, 2, 4},
			new int[] {8, 3, 1}
		};
		int[][] matrix2 = new int[][] {
			new int[] {3, 5},
			new int[] {2, 8},
			new int[] {7, 9}
		};
		int[][] mul = new int[][] {
			new int[] {0, 0},
			new int[] {0, 0}
		};

		int rows1 = matrix1.Length;
		int cols2 = matrix2[0].Length;

		// mul[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] 

		for (int i = 0; i < rows1; i++) {
			for (int j = 0; j < cols2; j++) {
				for (int k = 0; k < COLS1; k++) {
					mul[i][j] += matrix1[i][k] * matrix2[k][j];

					Console.Write("mul[" + i);
					Console.Write("][" + j);
					Console.Write("] += m1[" + i);
					Console.Write("][" + k);
					Console.Write("] * m2[" + k);
					Console.Write("][" + j + "]\n");
				}
				Console.Write("\n");
			}
		}
		
		for (int i = 0; i < COLS2; i++) {
			for (int j = 0; j < COLS2; j++) {
				Console.Write(mul[i][j] + " ");
			}
			Console.Write("\n");
		}
	}
}




/*
run:
  
mul[0][0] += m1[0][0] * m2[0][0]
mul[0][0] += m1[0][1] * m2[1][0]
mul[0][0] += m1[0][2] * m2[2][0]

mul[0][1] += m1[0][0] * m2[0][1]
mul[0][1] += m1[0][1] * m2[1][1]
mul[0][1] += m1[0][2] * m2[2][1]

mul[1][0] += m1[1][0] * m2[0][0]
mul[1][0] += m1[1][1] * m2[1][0]
mul[1][0] += m1[1][2] * m2[2][0]

mul[1][1] += m1[1][0] * m2[0][1]
mul[1][1] += m1[1][1] * m2[1][1]
mul[1][1] += m1[1][2] * m2[2][1]

44 72 
37 73 
  
*/

 



answered Sep 15, 2022 by avibootz

Related questions

2 answers 229 views
229 views asked Jun 1, 2014 by avibootz
1 answer 51 views
1 answer 49 views
1 answer 49 views
1 answer 51 views
51 views asked Oct 1, 2025 by avibootz
...