2016-10-08 272 views
0

我有一個2x2和一個2x1矩陣,並希望將它們相乘。這是代碼。矩陣乘法2x2 2x1 C#

for (i = 0; i < row; i++) 
     { 
      for (j = 0; j < col; j++) 
      { 
       for (k = 0; k < 2; k++) 
       { 
        mulMatrix[k] = Mat1[i, j] * Mat2[j]; 
       } 
      } 
     } 

回答

0

我覺得.NET已經有了一個庫。在這裏閱讀:https://msdn.microsoft.com/en-us/library/system.windows.media.matrix.multiply(v=vs.110).aspx

基本上,例子是:

private void multiplicationExample() 
{ 

    Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30); 
    Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12); 

    // matrixResult is equal to (70,100,150,220,240,352) 
    Matrix matrixResult = Matrix.Multiply(matrix1, matrix2); 

    // matrixResult2 is also 
    // equal to (70,100,150,220,240,352) 
    Matrix matrixResult2 = matrix1 * matrix2; 


} 
+0

3x3矩陣乘法是不一樣的事情發生了什麼問 –

+0

這個庫是不支持的在VS 2012 – ahsan

0

嘗試math.net。這裏有一個例子:

var matrixA = DenseMatrix.OfArray(new[,] { { 1.0, 2.0 }, { 4.0, 5.0 } }); 
var matrixB = DenseMatrix.OfArray(new[,] { { 1.0 }, { 2.0 } }); 

var result = matrixA * matrixB; 
Console.WriteLine(result); 

See this .NET Fiddle