1.Matrix Addition:
public static void main(String[] args) {int[][] a= {{1,2,3}{1,2,3},{4,5,6}};int[][] b= {{6,7,8},{1,2,3},{7,8,9}};int [][] c=new int[a.length][a[0].length];for(int row=0;row<a.length;row++){for(int col=0;col<b.length;col++){c[row][col]=a[row][col]+b[row][col];System.out.print(c[row][col]+" ");}System.out.println();}}public static void main(String[] args) { int[][] a= {{1,2,3} {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { c[row][col]=a[row][col]+b[row][col]; System.out.print(c[row][col]+" "); } System.out.println(); } }public static void main(String[] args) { int[][] a= {{1,2,3} {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { c[row][col]=a[row][col]+b[row][col]; System.out.print(c[row][col]+" "); } System.out.println(); } }
Enter fullscreen mode Exit fullscreen mode
output:
7 9 11
2 4 6
11 13 15
2.Martix Multipliction:
public static void main(String[] args) {int[][] a= {{1,2,3},{1,2,3},{4,5,6}};int[][] b= {{6,7,8},{1,2,3},{7,8,9}};int [][] c=new int[a.length][a[0].length];for(int row=0;row<a.length;row++){for(int col=0;col<b.length;col++){for(int k = 0;k<b.length;k++){c[row][col]=c[row][col]+(a[row][k]*b[k][col]);}System.out.print(c[row][col]+" ");}System.out.println();}}public static void main(String[] args) { int[][] a= {{1,2,3}, {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { for(int k = 0;k<b.length;k++) { c[row][col]=c[row][col]+(a[row][k]*b[k][col]); } System.out.print(c[row][col]+" "); } System.out.println(); } }public static void main(String[] args) { int[][] a= {{1,2,3}, {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { for(int k = 0;k<b.length;k++) { c[row][col]=c[row][col]+(a[row][k]*b[k][col]); } System.out.print(c[row][col]+" "); } System.out.println(); } }
Enter fullscreen mode Exit fullscreen mode
output:
29 35 41
29 35 41
71 86 101
© 版权声明
THE END
暂无评论内容