1、矩阵的转置:设计一个3X4的矩阵C,输入该矩阵,并且输入该矩阵的转置C’
2、实现两个矩阵的乘积(A*B)和点乘(A.*B),示例如下:
//lsy package java_experiments; import java.util.Scanner; public class four_one { public static void transform() { Scanner input = new Scanner(System.in); int[][] a = new int[3][4]; int[][] b = new int[4][3]; System.out.println("输入三行四列的矩阵:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { int num = input.nextInt(); a[i][j] = num; b[j][i] = a[i][j]; } } System.out.println("转置前:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "t"); } System.out.println(); } System.out.println("转置后:"); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i].length; j++) { System.out.print(b[i][j] + "t"); } System.out.println(); } input.close(); } public static void main(String[] args) { // TODO Auto-generated method stub transform(); } }实验结果: 内容二代码:
//lsy package java_experiments; import java.util.Scanner; public class four_two { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入第一个矩阵行数和列数:"); int i1 = input.nextInt(); int j1 = input.nextInt(); int[][] a = new int[i1][j1]; System.out.println("输入第一个矩阵:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { int num = input.nextInt(); a[i][j] = num; } } System.out.println("输入第二个矩阵行数和列数:"); int i2 = input.nextInt(); int j2 = input.nextInt(); int[][] b = new int[i2][j2]; System.out.println("输入第二个矩阵:"); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i].length; j++) { int num = input.nextInt(); b[i][j] = num; } } Cheng(a, b); System.out.println("==============="); DianCheng(a, b); input.close(); } // 矩阵乘矩阵 public static void Cheng(int a[][], int b[][]) { int[][] c = new int[a.length][b[0].length]; if (a[0].length == b.length) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { for (int k = 0; k < b.length; k++) { c[i][j] += a[i][k] * b[k][j]; } } } } else { System.out.println("A 的列数不等于 B 的行数"); } System.out.println("矩阵相乘后:"); for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { System.out.print(c[i][j] + "t"); } System.out.println(); } } public static void DianCheng(int a[][], int b[][]) { int[][] c = new int[a.length][b[0].length]; if ((a.length == b.length) && (a[0].length == b[0].length)) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { c[i][j] = a[i][j] * b[i][j]; } } System.out.println("矩阵点乘后:"); for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { System.out.print(c[i][j] + "t"); } System.out.println(); } } else System.out.println("矩阵无法进行点乘"); } }实验结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)