C#实现矩阵乘法实例分析
发布时间:2020-12-15 05:49:27 所属栏目:百科 来源:网络整理
导读:本篇章节讲解C#实现矩阵乘法的方法。供大家参考研究。具体如下: static double[][] MatrixMultiplication(double[][] matrixOne,double[][] matrixTwo){ int aRows = matrixOne.Length; int aCols = matrixOne[0].Length; int bRows = matrixTwo.Le
本篇章节讲解C#实现矩阵乘法的方法。分享给大家供大家参考。具体如下: static double[][] MatrixMultiplication(double[][] matrixOne,double[][] matrixTwo) { int aRows = matrixOne.Length; int aCols = matrixOne[0].Length; int bRows = matrixTwo.Length; int bCols = matrixTwo[0].Length; if (aCols != bRows) throw new Exception("Out of shape matrices"); double[][] result = CreateMatrix(aRows,bCols); for (int i = 0; i < aRows; ++i) // each row of MatrixOne for (int j = 0; j < bCols; ++j) // each col of MatrixTwo for (int k = 0; k < aCols; ++k) result[i][j] += matrixOne[i][k] * matrixTwo[k][j]; return result; } static double[][] CreateMatrix(int rows,int cols) { double[][] result = new double[rows][]; for (int i = 0; i < rows; ++i) result[i] = new double[cols]; return result; } 希望本文所述对大家的C#程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |