添加和乘以2D矩阵

时间:2015-04-22 04:18:07

标签: java arrays matrix

我对我目前的一个CS课程的家庭作业感到非常困惑。

问题是要创建一个方法来乘以两个3x3矩阵。除了创建另一种添加两个3x3矩阵的方法,然后将两者结合起来并显示输出。总共有3个矩阵,所有用户都输入了。

矩阵1:(1,2,3,4,5,6,7,8,9)

矩阵2:(9,8,7,6,5,4,3,2,1)

矩阵3:(0,2,4,1,4.5,2.2,1.1,4.3,5.2)

结果输出:(30,26,22,85,73.5,56.2,139.1,118.3,95.2)

在给出的输出中,矩阵1 *矩阵2 +矩阵3 =输出。

我的主要问题是在输出中显示正确的数字,看起来非常简单,但他的数字完全取决于我的数字。我非常难过你只加倍并加一次,但如果这是真的话,没有两个数字可以等于139.1。

    import java.util.Scanner    
    public class Assignment8 {

      public static void printResult(                                                        
              double[][] m1, double[][] m2, double[][] m3, double[][] resultMatrix, char op1, char op2) {
            for (int i = 0; i < m1.length; i++) {
              for (int j = 0; j < m1[0].length; j++)
                System.out.print(" " + m1[i][j]);

              if (i == m1.length / 2)
                System.out.print( "  " + op1 + "  " );
              else
                System.out.print( "     " );

              for (int j = 0; j < m2[0].length; j++)
                System.out.print(" " + m2[i][j]);

              if (i == m1.length / 2)
                System.out.print( "  " + op2 + "  " );
              else
                System.out.print( "     " );

              for (int j = 0; j < m3[0].length; j++)
                System.out.print(" " + m3[i][j]);

              if (i == m1.length / 2)
                System.out.print( "  =  " );
              else
                System.out.print( "     " );

              for (int j = 0; j < resultMatrix[0].length; j++)
                System.out.print(" " + resultMatrix[i][j]);

              System.out.println();
            }
    }


    public static double[][] multiplyMatrix(double[][] m3, double[][] m4) {
        double[][] result = new double[m3.length][m3[0].length];
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++)
                    result[i][j] = m3[i][j] * m4[i][j];
        }
                    return result;
    }

    public static double[][] addMatrix(double[][] m1, double[][] m2) {
        double[][] result = new double[m1.length][m1[0].length];
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++)
                    result[i][j] = m1[i][j] + m2[i][j];
        }
                    return result;
    }


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double[][] m1 = new double[3][3];
        double[][] m2 = new double[3][3];
        double[][] m3 = new double[3][3];

        System.out.print("Enter Matrix 1: ");   
        for(int row = 0; row <m1.length; row++){
            for(int column = 0; column < m1[row].length; column++) {
                m1[row][column] = input.nextDouble();
            }
        }

        System.out.print("Enter Matrix 2:");
        for(int row = 0; row <m2.length; row++){
            for(int column = 0; column < m2[row].length; column++) {
                m2[row][column] = input.nextDouble();
            }
        }

        System.out.print("Enter Matrix 3:");
        for(int row = 0; row <m3.length; row++){
            for(int column = 0; column < m3[row].length; column++) {
                m3[row][column] = input.nextDouble();
            }
        }
        double multiply[][] = multiplyMatrix(m1, m2);
        double add[][] = addMatrix(multiply, m3);
        double resultMatrix[][] = addMatrix(add, multiply);

        printResult(m1, m2, m3, resultMatrix, '*', '+');        
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你用来乘以你的矩阵的逻辑是错误的。看看this

答案 1 :(得分:0)

问题解决了,凤凰城向我指出了正确的方向,我只是编辑了乘法方法以便正确执行,而且尤里卡有效!