【问题标题】:Multipying a Matrix by the same power using Java使用Java将矩阵乘以相同的幂
【发布时间】:2016-10-06 01:05:09
【问题描述】:

我有以下代码。我希望陈述一个矩阵并将其乘以特定的幂。

import java.util.Scanner;

class Test
{
   public static void main(String args[])
   {
      int m, n, c, d, k, Ind, x;
      double sum = 0;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter the power by which you want the Matrix to be multiplyed by: ");
      Ind = in.nextInt();

      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();

      double Mtr[][] = new double[m][n];
      double multiply[][] = new double[m][n];

      System.out.println("Enter the elements of first matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            Mtr[c][d] = in.nextInt();

      if ( Ind ==1 )

            for ( c = 0 ; c < m ; c++ )
             {
                for ( d = 0 ; d < n ; d++ )
                   System.out.print(Mtr[c][d]+"\t");

                System.out.print("\n");
             }

          else
          {           

         for ( c = 0 ; c < m ; c++ )
         {
             for (x=0; x < Ind; x++)
             {
                 for ( d = 0 ; d < n ; d++ )
                 {       
                         for ( k = 0 ; k < m ; k++ )
                         {

                         sum = sum + Mtr[c][k]*Mtr[k][d];
                         }

                         multiply[c][d] = sum;
                     sum = 0;
                 } 
             }
         }

             System.out.println("Product of entered matrices:-");

             for ( c = 0 ; c < m ; c++ )
             {
                for ( d = 0 ; d < n ; d++ )
                   System.out.print(multiply[c][d]+"\t");

                System.out.print("\n");
             }

        }

   }
}

我遇到的问题是我不确定在哪里应用循环以将矩阵乘以我输入的幂。我想我可能还必须更改逻辑,以便

  sum = sum + Mtr[c][k]*multiply[k][d]

【问题讨论】:

  • 你期待什么输出,你得到什么?
  • 如果我输入 2 x 2 矩阵 {1 2} {3 4}。我得到结果 {7 10} {15 22}。不管我输入什么力量。我期望结果是三的幂 {37 54} {81 128}。
  • stackoverflow.com/questions/22900872/… 我希望这会有所帮助。
  • 对您的要求不够清楚。你能解释一下你是如何得到这些输出值的吗?我可以看看你是否取 3^3 = 27 并将这个数字与每个矩阵项相乘,得到 {27 54} {81 108}。如果我错了,请纠正我。
  • 如果我对矩阵 {1 2} {3 4} 进行平方。设 A 为 {1 2} {3 4} 且 B 为 {1 2}{3 4} 那么结果 [0][0] 值将是 [0][0]xb[0][0] + a [0][1] xb[1][0] = 1x1 + 2x3 = 7。整体结果矩阵将是 {7 10} {15 22}。现在,如果我再次将结果乘以 {1 2} {3 4}(即 {1 2} {3 4} 的立方),那么结果 [0][0] 的值将是 7x1 + 10x3 = 37。总体结果 { 37 54} {81 118}。我正在尝试获得这些结果(分别为 power = 2 和 power = 3)

标签: java arrays matrix indices


【解决方案1】:

试试这个:

import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {

        int[][] a = new int[][] { { 1, 2 }, { 3, 4 } }; // first array
        int[][] b = a;                                  
        int[][] c = a;                                  
        System.out.println("Eneter the multilpicity : ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(n < 2){                                      // As we need minimum two matrix for multiplication
                                                        // Incase given multiplicity less than 2 it will not produce any difference 
            for (int i = 0; i < c.length; i++) {
                for (int j = 0; j < c[0].length; j++) {
                    System.out.print(c[i][j] + " ");
                }
                System.out.println();

            }
            System.exit(0);
        }else if (n > 2) {                          
            while (n >= 2) {                        
                c = multiply(c, b); n--;
            }
        } else {
            c = multiply(c, b);                     
        }

        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c[0].length; j++) {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] multiply(int[][] a, int[][] b) {
        int rowsInA = a.length;
        int columnsInA = a[0].length; // same as rows in B
        int columnsInB = b[0].length;
        int[][] c = new int[rowsInA][columnsInB];
        for (int i = 0; i < rowsInA; i++) {
            for (int j = 0; j < columnsInB; j++) {
                for (int k = 0; k < columnsInA; k++) {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        return c;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2020-07-18
    • 1970-01-01
    • 2015-12-31
    • 2015-01-24
    • 2016-12-11
    相关资源
    最近更新 更多