一、介绍1、介绍。 矩阵的运算,本质上就是二维数组的运算。2、公共代码。/** * 打印二维数组 * * @param a 二维数组a */public static void print(double[][] a) { print(a, 5, 0);} /** * 打印二维数组 * * @param a 二维数组a * @param precision 如果是浮点型的话,保留几位小数 */public static void print(int[][] a, int width, int precision) { String str = "%" + width + "d "; for (int i = 0; i < a.length; i++) { int[] aItem = a[i]; for (int j = 0; j < aItem.length; j++) { System.out.print(String.format(str, aItem[j])); } System.out.println(); }} /** * 打印二维数组 * * @param a 二维数组a * @param precision 如果是浮点型的话,保留几位小数 */public static void print(double[][] a, int width, int precision) { String str = "%" + width + "." + precision + "f "; for (int i = 0; i < a.length; i++) { double[] aItem = a[i]; for (int j = 0; j < aItem.length; j++) { System.out.print(String.format(str, aItem[j])); } System.out.println(); }}二、减法。1、介绍。 \LARGE \begin{bmatrix}a&b\\ c&d\end{bmatrix} - \begin{bmatrix}e&f\\ g&h\end{bmatrix} = \begin{bmatrix}a-e&b-f\\ c-g&d-h\end{bmatrix}2、代码。/** * 矩阵a和b对应位置的数相减 * * @param a 矩阵a[m][n] * @param b 矩阵b[n][p] */public static double[][] sub(double[][] a, double[][] b) throws Exception { int row = a.length; int column = a[0].length; int bRow = b.length; int bColumn = b[0].length; if (row != bRow || column != bColumn) { throw new Exception("两个矩阵的大小不一致"); } double[][] result = new double[row][column]; for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) result[i][j] = a[i][j] - b[i][j]; return result;}public static void main(String[] args) { try { double[][] a = new double[][]{ {1, 2, 3, 4}, {5, 6, 7, 8}, {1, 1, 2, 2}, {3, 3, 4, 4}, }; System.out.println("矩阵a:"); ArrayUtils.print(a); do