矩阵乘法(java)

类别:Java 点击:0 评论:0 推荐:

//MultiplyMatrix by seabridge 2004-9-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 ("");
    }
     }   
}

本文地址:http://com.8s8s.com/it/it15742.htm