求C#矩阵相乘的例子
2个回答
推荐于2018-04-04
展开全部
using System;
public class MatrixMultiply
{
public static void Main()
{
int a,b,c,d;
Console.WriteLine ("该程序将求出两个矩阵的积:");
Console.WriteLine ("请指定矩阵A的行数:");
a=int.Parse (Console.ReadLine ());
Console.WriteLine ("请指定矩阵A的列数:");
b=int.Parse (Console.ReadLine ());
int[,] MatrixA=new int [a,b];
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
Console.WriteLine ("请输入矩阵A第{0}行第{1}列的值:",i+1,j+1);
MatrixA[i,j]=int.Parse (Console.ReadLine ());
}
}
Console.WriteLine ("矩阵A输入完毕.");
Console.WriteLine ("请指定矩阵B的行数:");
c=int.Parse (Console.ReadLine ());
Console.WriteLine ("请指定矩阵B的列数:");
d=int.Parse (Console.ReadLine ());
int[,] MatrixB=new int [c,d];
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
{
Console.WriteLine ("请输入矩阵A第{0}行第{1}列的值:",i+1,j+1);
MatrixB[i,j]=int.Parse (Console.ReadLine ());
}
}
Console.WriteLine ("矩阵B输入完毕.");
Console.WriteLine ("矩阵A为:");
outputMatrix(MatrixA,a,b);
Console.WriteLine ("矩阵B为:");
outputMatrix(MatrixB,c,d);
if(b!=c)
{
Console.WriteLine ("矩阵A的列数与矩阵B的行数不相等,无法进行乘积运算!");
return;
}
else
{
Console.WriteLine ("矩阵A与矩阵B的乘积为:");
}
int[,] MatrixC=new int [a,d];
for(int i = 0; i < a; i++)
{
for(int j = 0; j < d; j++)
{
MatrixC[i,j] = 0;
for(int k = 0; k < b; k++)
{
MatrixC[i,j] += MatrixA[i,k] * MatrixB[k,j];
}
}
}
outputMatrix(MatrixC,a,d);
}
private static void outputMatrix(int[,] MatrixX, int rowCount, int columnCount)
{
for(int i = 0; i < rowCount; i++)
{
for(int j = 0; j < columnCount; j++)
{
Console.Write(MatrixX[i,j] + "\t");
}
Console.WriteLine();
}
}
}
。。有什么不懂的可以问我。。嘿嘿。
public class MatrixMultiply
{
public static void Main()
{
int a,b,c,d;
Console.WriteLine ("该程序将求出两个矩阵的积:");
Console.WriteLine ("请指定矩阵A的行数:");
a=int.Parse (Console.ReadLine ());
Console.WriteLine ("请指定矩阵A的列数:");
b=int.Parse (Console.ReadLine ());
int[,] MatrixA=new int [a,b];
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
Console.WriteLine ("请输入矩阵A第{0}行第{1}列的值:",i+1,j+1);
MatrixA[i,j]=int.Parse (Console.ReadLine ());
}
}
Console.WriteLine ("矩阵A输入完毕.");
Console.WriteLine ("请指定矩阵B的行数:");
c=int.Parse (Console.ReadLine ());
Console.WriteLine ("请指定矩阵B的列数:");
d=int.Parse (Console.ReadLine ());
int[,] MatrixB=new int [c,d];
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
{
Console.WriteLine ("请输入矩阵A第{0}行第{1}列的值:",i+1,j+1);
MatrixB[i,j]=int.Parse (Console.ReadLine ());
}
}
Console.WriteLine ("矩阵B输入完毕.");
Console.WriteLine ("矩阵A为:");
outputMatrix(MatrixA,a,b);
Console.WriteLine ("矩阵B为:");
outputMatrix(MatrixB,c,d);
if(b!=c)
{
Console.WriteLine ("矩阵A的列数与矩阵B的行数不相等,无法进行乘积运算!");
return;
}
else
{
Console.WriteLine ("矩阵A与矩阵B的乘积为:");
}
int[,] MatrixC=new int [a,d];
for(int i = 0; i < a; i++)
{
for(int j = 0; j < d; j++)
{
MatrixC[i,j] = 0;
for(int k = 0; k < b; k++)
{
MatrixC[i,j] += MatrixA[i,k] * MatrixB[k,j];
}
}
}
outputMatrix(MatrixC,a,d);
}
private static void outputMatrix(int[,] MatrixX, int rowCount, int columnCount)
{
for(int i = 0; i < rowCount; i++)
{
for(int j = 0; j < columnCount; j++)
{
Console.Write(MatrixX[i,j] + "\t");
}
Console.WriteLine();
}
}
}
。。有什么不懂的可以问我。。嘿嘿。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-31
展开全部
using System;public class MatrixMultiply
{
public static void Main()
{
int[,] matrixA = new int[2, 3]; int[,] matrixB = new int[3, 4] { { 4, 2, 1, 7 }, { 3, 6, 1, 0 }, { 5, 3, 2, 4 } }; int[,] matrixC = new int[2, 4]; for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
matrixA[i, j] = (i + 1) * (j + 1) + 1;
}
}
for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++)
{
matrixC[i, j] = 0; for (int k = 0; k < 3; k++)
{
matrixC[i, j] += matrxA[i, k] * matrixB[k, j]; // [0,1]*[1,0]+[0,1]*[1,0]+[0,2]*[2,0]=86 }
}
}
Console.WriteLine("\n********矩阵A********");
outputMatrix(matrixA, 2, 3);
Console.WriteLine("\n********矩阵B********");
outputMatrix(matrixB, 3, 4);
Console.WriteLine("\n********矩阵A X 矩阵B********");
outputMatrix(matrxC, 2, 4);
Console.Read();
}
private static void outputMatrix(int[,] matrixX, int rowCount, int columnCount)
{
if (matrixX.Length != rowCount * columnCount)
{
Console.WriteLine("行列数错误!");
return;
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
Console.Write(matrixX[i, j] + "\t");
}
Console.WriteLine();
}
}
}
{
public static void Main()
{
int[,] matrixA = new int[2, 3]; int[,] matrixB = new int[3, 4] { { 4, 2, 1, 7 }, { 3, 6, 1, 0 }, { 5, 3, 2, 4 } }; int[,] matrixC = new int[2, 4]; for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
matrixA[i, j] = (i + 1) * (j + 1) + 1;
}
}
for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++)
{
matrixC[i, j] = 0; for (int k = 0; k < 3; k++)
{
matrixC[i, j] += matrxA[i, k] * matrixB[k, j]; // [0,1]*[1,0]+[0,1]*[1,0]+[0,2]*[2,0]=86 }
}
}
Console.WriteLine("\n********矩阵A********");
outputMatrix(matrixA, 2, 3);
Console.WriteLine("\n********矩阵B********");
outputMatrix(matrixB, 3, 4);
Console.WriteLine("\n********矩阵A X 矩阵B********");
outputMatrix(matrxC, 2, 4);
Console.Read();
}
private static void outputMatrix(int[,] matrixX, int rowCount, int columnCount)
{
if (matrixX.Length != rowCount * columnCount)
{
Console.WriteLine("行列数错误!");
return;
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
Console.Write(matrixX[i, j] + "\t");
}
Console.WriteLine();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询