2012-04-19 132 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