首页 > 学院 > 开发设计 > 正文

用Java的循环实现矩阵乘法代码分享

2019-11-18 12:02:49
字体:
来源:转载
供稿:网友

  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 ("");
  }
  }
  }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表