PoeticalJustice

导航

矩阵运算(二维数组)

 1 package cn.bjsxt.array2;
 2 /**
 3  * 
 4  * 矩阵加法
 5  * 乘法
 6  * @author Administrator
 7  *
 8  */
 9 public class Matrix {
10     public static void print(int[][] c){
11         //打印矩阵
12         for(int i=0;i<c.length;i++){
13             for(int j=0;j<c.length;j++){
14                 System.out.print(c[i][j]+"\t");
15             }
16             System.out.println();
17         }
18     }
19     public static int[][] add(int[][]a,int[][]b){
20         //加法
21         int[][]c=new int[a.length][a.length];
22         for(int i=0;i<c.length;i++){
23             for(int j=0;j<c.length;j++){
24                 c[i][j]=a[i][j]+b[i][j];
25             }
26         }
27         return c;
28     }
29     //矩阵乘法
30     public static int[][] multiply(int[][]a,int[][]b){
31         //加法
32         int[][]c=new int[a.length][a.length];
33         for(int i=0;i<c.length;i++){
34             for(int j=0;j<c.length;j++){
35                 c[i][j]=a[i][j]*b[i][j];
36             }
37         }
38         return c;
39     }
40     
41     public static void main(String[] args) {
42         
43     
44     int[][] a ={ 
45                     {1,3,3},
46                     {2,4,7},
47                     {6,4,9}
48                 };
49     int[][]    b = {
50                     {3,3,3},
51                     {2,4,7},
52                     {1,5,7}
53                 };
54     
55     
56         int[][]    c = add(a,b);
57     
58         print(c);
59         System.out.println("#############################");
60         
61         int[][]d = multiply(a,b);
62         print(d);
63 
64 
65     }
66 }

 

posted on 2017-10-04 17:54  PoeticalJustice  阅读(829)  评论(0编辑  收藏  举报