2012-04-19 136 views
0

我有一個問題,基本上,我有兩個矩陣(矢量),一個巨大的矩陣和一個較小的矩陣。我有分割大量基質成塊有趣的算法來比較兩個矩陣?

因此,例如(小塊的大小的)(我使用的測試數據在這裏),所以大量的矩陣尺寸是一種算法:4x4和小矩陣是2×2和那麼我將特定塊(在當前位置)傳遞給一個函數,該函數檢查小矩陣是否等於大塊(在該特定位置),如果是,則返回true,否則返回false。

我可以輸出每塊這樣的:

bool compareMatrix(vector<double> &theMatrix1, vector<double> &theMatrix2, int startRow, int startCol) 
{ 
     // I can output the matrix blocks like this: 
     cout << theMatrix1[startRow*2+startCol] << endl;  
} 

但我不太明白我怎麼會比較塊(在startingRow/COL)的小矩陣..

如何它將是這樣的: 矩陣1:(4×4)

0 1 0 1 
1 1 0 1 
0 0 1 1 
0 1 1 1 

矩陣2:(2×2)

0 1 
0 1 

我然後塊分割成2×2:

B1 =

0 1 
1 1 

是B1等於theMatrix2 - 無所以返回false

B2 =

0 1 
0 1 

是B2等於theMatrix2 - 是的,所以返回true

我真的很想盡我所能解釋一些細節問題,希望有人能給我一些建議,因爲我一直在努力研究這麼久!

感謝

+1

我不明白你在做什麼。你是否試圖檢查更小的矩陣是否以較大的方式被「包含」?更嚴謹的問題陳述可以幫助您獲得我認爲的反饋。 – 2012-04-19 14:44:13

+0

你可以定義一個'BOOL areEqual = true',然後遍歷所有元素並單獨比較它們,如果兩個元素不相同,你可以設置'areEqual = false'並跳出循環。 – 2012-04-19 14:45:19

+2

4x4矩陣有四個2x2矩陣?或九? – Memming 2012-04-19 14:46:16

回答

0

如果大矩陣的大小是已知的,你可以用你的2x2矩陣,像這樣

int bigMatrixSize=4; 

bool compare(...) 
{ 
for (int i=0; i<2; ++i) 
    for (int k=0; k<2; ++k) 
     if(bigMatrix[k+startX+(i+staryY)*bigMatrixSize] != smallMatrix[k][i]) 
      return false; 
return true; 
} 

我離開了邊界檢查和一些其他的東西比較它的一小部分,但它應該給你一個想法。

0
bool compareMatrix(vector<double> &theMatrix1, int nRow1, int nCol1, vector<double> &theMatrix2, int nRow2, int nCol2, int startRow, int startCol) 
{ 
    int p1 = startRow * nCol1 + startCol, p2 = 0; 

    for (int y = 0; y < nRow2; ++y) 
    { 
     for (int x = 0; x < nCol2; ++x) 
     { 
      if (theMatrix1[p1 + x] != theMattrix2[p2 + x]) // You can use memcmp here, but it's safer let compiler do the optimization. 
      { 
       return false; 
      } 
     } 

     p1 += nCol1; 
     p2 += nCol2; 
    } 

    return true; 
} 

你想要這樣的嗎?您可以將列數添加到位置以到達下一行。

+0

nRow1,nCol1會通過什麼? – Phorce 2012-04-19 15:04:04

+0

以防萬一您的矩陣大小不是4x4和2x2。像compareMatrix(m1,4,4,m2,2,2,1,1)。 – BlueWanderer 2012-04-19 15:07:02