试图写一个方法来乘法矩阵

时间:2012-10-17 06:30:27

标签: java matrix

public class Homework2 {
    public static void main(String[] args){
    int num1 = (int) (Math.random()*(10-3+1)+3);
    int num2 = (int) (Math.random()*(10-3+1)+3);

    double[][] doubMatrix1 = new double[num1][num2];
    double[][] doubMatrix2 = new double[num1][num2];
    double[][] doubMatrix3 = new double[num1][num2];

    doubMatrix1 = getdoubMatrix(num1,num2);
    doubMatrix2 = getdoubMatrix(num1,num2);
    doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2, num1, num2);        
    printDoubMatrix("First matrix", doubMatrix1);
    printDoubMatrix("Second matrix", doubMatrix2);
    printDoubMatrix("Result of adding", doubMatrix3);
    doubMatrix2 =transposeMatrix(num1,num2, doubMatrix2);
    printDoubMatrix("Second matrix transposed", doubMatrix2);
    doubMatrix3 = multipyMatrices(doubMatrix1, doubMatrix2, num1, num2);
    printDoubMatrix("Result of multiplying", doubMatrix3);




}

public static double[][] getdoubMatrix(int num1,int num2){

    double[][] tempArray = new double[num1][num2];
    for(int i = 0;i < tempArray.length;i++)
        for(int j = 0;j < tempArray[i].length;j++)
        {
            tempArray[i][j] = Math.random() * (100);                
        }
    return tempArray;
}

public static double[][] addMatrices(double[][] doubMatrix1, double[][] doubMatrix2,int num1,int num2)
{

    double[][] tempArray = null;
    if(doubMatrix1.length == doubMatrix2.length)
        if(doubMatrix1[0].length == doubMatrix2[0].length)
        {
             tempArray = new double[num1][num2];
            for(int i = 0; i< doubMatrix1.length;i++)
                  for(int j = 0; j< doubMatrix1[i].length;j++ )
                  {
                      tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j];
                  }
        }
        else
        {
            return tempArray = new double[0][0];

        }

    return tempArray;
}

public static void printDoubMatrix(String text,double[][] doubMatrix1){

    System.out.println(text);
    for(int i = 0; i< doubMatrix1.length;i++)
    {
          for(int j = 0; j< doubMatrix1[i].length;j++ ) {                    
              System.out.printf("%9.1f", doubMatrix1[i][j]);                
          }
          System.out.println();
    }       
}

public static double[][] transposeMatrix(int num1, int num2, double[][] doubMatrix2){
    double[][] tempArray = new double[num2][num1];
    for(int i = 0;i < num1;i++)
        for(int j = 0;j < num2;j++)
        {
            tempArray[j][i] = doubMatrix2[i][j];               
        }    
    return tempArray;
}

public static double[][] multipyMatrices(double[][] doubMat1, double[][] doubMat2, int num1, int num2)

{       
        double[][] tempArray = null;
        if(doubMat1[0].length == doubMat2.length)
        {
                tempArray = new double[num1][num2];
                for(int i = 0; i< doubMat1.length;i++)              
                    for(int j = 0; j < doubMat1[i].length;j++ )
                        for(int k = 0; k <doubMat1[i].length;k++ )
                    {
                            tempArray[i][j] = tempArray[i][j] + doubMat1[i][k]*doubMat2[k][j];

                    }
            }

            else
            {

                return tempArray = new double[0][0];
            }
        return tempArray;
}

}

好吧我已经将我的代码缩小到for循环中的一个错误,它在最后一个方法中说j,但我真的不知道如何修复它。任何人都可以建议一种方法来使这段代码工作。乘法矩阵的语句也没错,循环行也没有错误

我的multiply方法输出与我的转置方法相同的东西,这是不正确的,我认为J范围超过了doubMat2的第二个索引

这是我的输出

第一个矩阵

     55.8     58.8     18.6      5.0     34.6     97.6      8.8
      8.4     71.1     63.3     15.7     63.9     41.8     89.2
      1.0     54.5     36.5     68.5     55.8     21.3     40.1
     10.0     67.2     37.8     45.9     95.5     76.0     98.2
     11.2     20.8      8.3     78.3     44.8     81.2     32.8
     58.0      7.9     57.2     22.2     92.6     90.0     93.2
     73.7      5.2     20.1     13.3     73.0     30.3     63.2
      9.1     72.1     75.3     53.3     45.6     86.6     85.4
Second matrix

     12.2     76.0      7.3     87.8      8.9     23.6     67.1
     65.4     22.0     91.3     96.0     59.6     76.4     82.0
     51.2     29.5     39.2     70.9      6.8      1.5     78.0
     52.6     47.3     29.5      1.4     95.8     15.4     43.9
     27.1     79.7     26.7     14.6     47.0     52.6     80.4
     65.7     88.8     46.5     60.9     82.6      4.9      9.9
     47.9     21.7     55.4     67.1     26.3     90.1     90.0
     96.0     15.7     92.9     74.9     22.8     17.3     53.9
Result of adding

     68.0    134.8     25.9     92.8     43.4    121.1     75.8
     73.8     93.1    154.6    111.7    123.5    118.2    171.2
     52.2     84.0     75.7    139.4     62.6     22.8    118.0
     62.6    114.5     67.3     47.3    191.2     91.4    142.1
     38.3    100.6     35.0     92.9     91.8    133.7    113.2
    123.7     96.7    103.6     83.2    175.2     94.9    103.1
    121.6     26.9     75.5     80.4     99.3    120.4    153.2
    105.1     87.8    168.2    128.2     68.4    103.9    139.2
Second matrix transposed

     12.2     65.4     51.2     52.6     27.1     65.7     47.9     96.0
     76.0     22.0     29.5     47.3     79.7     88.8     21.7     15.7
      7.3     91.3     39.2     29.5     26.7     46.5     55.4     92.9
     87.8     96.0     70.9      1.4     14.6     60.9     67.1     74.9
      8.9     59.6      6.8     95.8     47.0     82.6     26.3     22.8
     23.6     76.4      1.5     15.4     52.6      4.9     90.1     17.3
     67.1     82.0     78.0     43.9     80.4      9.9     90.0     53.9
Result of multiplying

     12.2     65.4     51.2     52.6     27.1     65.7     47.9     96.0
     76.0     22.0     29.5     47.3     79.7     88.8     21.7     15.7
      7.3     91.3     39.2     29.5     26.7     46.5     55.4     92.9
     87.8     96.0     70.9      1.4     14.6     60.9     67.1     74.9
      8.9     59.6      6.8     95.8     47.0     82.6     26.3     22.8
     23.6     76.4      1.5     15.4     52.6      4.9     90.1     17.3
     67.1     82.0     78.0     43.9     80.4      9.9     90.0     53.9

0 个答案:

没有答案