2015-07-20 120 views
0

特定元素我有兩個矩陣相乘以矩陣

A = {{1,2,3}, 
    {4,5,6}, 
    {7,8,9}}; 

B = {{1,2,3,10}, 
    {4,5,6,11}, 
    {7,8,9,12}, 
    {1,2,3,13}}; 

我想將它們相乘,以下列方式。 來自矩陣A的元素{5,6},{8,9}乘以{1,2},{4,5}(來自B的元素)。我知道我可以乘以個人指數,但我怎樣才能循環使用它們?

我不知道如何解決這個問題。如果有人能給我一個提示,這將是一個很大的幫助。

我不是在尋找答案。關於如何使用循環執行此操作的簡單邏輯就足夠了。

+3

你嘗試過什麼? –

+0

我已經嘗試過pesudo代碼,但是我現在無法在我的頭上得到它 – Maddy

+0

你打算做矩陣乘法嗎? –

回答

0

好像你不是來聊天。對不起,但我必須跑。 這裏是一個可能的解決問題的方法:

int A[][] = new int[][]{{1,2,3}, 
          {4,5,6}, 
          {7,8,9}}; 
    int B[][] = new int[][]{{1,2,3,10}, 
          {4,5,6,11}, 
          {7,8,9,12}, 
          {1,2,3,13}}; 

    System.out.println("===================="); 
    int number = 1; 
    for(int rowIndex = A.length - 2; rowIndex < A.length; rowIndex++) { 
     for(int colIndex = A[0].length - 2; colIndex < A[0].length; colIndex++) { 
      number *= A[rowIndex][colIndex]; 
      System.out.print(A[rowIndex][colIndex] + "\t"); 
     } 
     System.out.print("\n"); 
    } 
    System.out.println("====================");  
    for(int rowIndex = 0; rowIndex < 2; rowIndex++) { 
     for(int colIndex = 0; colIndex < 2; colIndex++) { 
      number *= B[rowIndex][colIndex]; 
      System.out.print(B[rowIndex][colIndex] + "\t"); 
     } 
     System.out.print("\n"); 
    } 
    System.out.println("===================="); 
    System.out.print("ANSWER IS: " + number); 

輸出:

==================== 
5 6 
8 9 
==================== 
1 2 
4 5 
==================== 
ANSWER IS: 86400 
+0

在我以前的答案有一個錯誤... A [0] .length是列的長度,而不是行長度(這是A.長度)代碼現在已修復...對不起有關 – Constantin

+0

你是如何得到86400 ? – Maddy

+1

我把所有的數字都乘上你想要的。 5x6x8x9 x 1x2x4x5 – Constantin