用Java的循环实现矩阵乘法代码分享
2008-01-05 10:50:37 来源:WEB开发网核心提示:public class MultiMatrix {int[][] multiplyMatrix;public static void main(String args[]){int[][] a={{1,0,3,-1},{2,1,0,2}};int[][] b={{4,1,0},{-1,1,3},{2,0,1},{1,
public class MultiMatrix {
int[][] multiplyMatrix;
public static void main(String args[]){
int[][] a={{1,0,3,-1},{2,1,0,2}};
int[][] b={{4,1,0},{-1,1,3},{2,0,1},{1,3,4}};
MultiMatrix mm=new MultiMatrix();
mm.mMatrix(a,b);
mm.display();
}
public void mMatrix(int[][] a,int[][] b){
multiplyMatrix=new int[a.length][b[0].length];
for (int i = 0; i<a.length; i++) {//rows of a
for (int j = 0; j<b[0].length; j++) {//columns of b
for (int k = 0; k<a[0].length; k++) {//columns of a = rows of b
multiplyMatrix[i][j]=multiplyMatrix[i][j]+a[i][k]*b[k][j];
}
}
}
}
public void display(){
for (int i = 0; i<multiplyMatrix.length; i++) {
for (int j = 0; j<multiplyMatrix[0].length; j++) {
System.out.PRint (multiplyMatrix[i][j]+" ");
}
System.out.println ("");
}
}
}
更多精彩
赞助商链接