有趣的算法比较两个矩阵? [英] Interesting algorithm to compare two matrices?

查看:175
本文介绍了有趣的算法比较两个矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,基本上,我有两个矩阵(向量),一个大型矩阵和一个较小的矩阵。我有一个算法,将大块矩阵分割成块(小块的大小)



例如(我在这里使用测试数据),所以大块矩阵大小为:4×4,小矩阵为2×2,然后I将特定块(在当前位置)传递到检查小矩阵是否等于大块(在该特定位置)的函数,如果是,然后返回true否则返回false。



我可以像这样输出每个块:

  bool compareMatrix lt; double>& theMatrix1,vector< double>& theMatrix2,int startRow,int startCol)
{
//我可以输出矩阵块如下:
cout< ; theMatrix1 [startRow * 2 + startCol]<< endl;
}

但我不太明白如何比较块



如何是这样:
Matrix 1:(4x4)

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

Matrix 2:(2x2)

  0 1 
0 1

然后我将块分割为2x2:



B1 =

  0 1 
1 1

是B1等于TheMatrix2-否则返回false



B2 =

  0 1 
0 1

是B2等于Matrix2 - 是的返回true



我真的试图解释事情



感谢

div class =h2_lin>解决方案

  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 =

for(int y = 0; y {
for(int x = 0; x if(theMatrix1 [p1 + x]!= theMattrix2 [p2 + x])//你可以在这里使用memcmp,但更安全的是让编译器做优化。
{
return false;
}
}

p1 + = nCol1;
p2 + = nCol2;
}

return true;
}

你想要这样吗?您可以将列数添加到到达下一行的位置。


I have a problem, basically, I have two matrices (vectors), one massive matrix and a smaller matrix. I have an algorithm that splits the massive matrix into blocks (of the size of the small block)

So for example (I am using test data here) so the massive matrix size is: 4x4 and the small matrix is 2x2 and then I pass the particular block (at the current position) to a function that checks to see if the small matrix is equal to the massive block (at that particular position) if it is, then returns true otherwise returns false.

I can output each block like this:

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;    
}

But I don't quite understand how I would compare the block (at the startingRow/Col) to the small matrix..

How it would is this: Matrix 1: (4x4)

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

Matrix 2: (2x2)

0 1 
0 1

I then split the blocks into 2x2:

B1 =

0 1 
1 1

is B1 equal to theMatrix2 - No so return false

B2 =

0 1
0 1

is B2 equal to theMatrix2 - Yes so return true

I have really tried to explain things to the best of detail as I possibly can and hope someone can give me some advice because I've been working on it for so long now!

Thanks

解决方案

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;
}

You want something like this? You can add the columns count to the position to reach the next row.

这篇关于有趣的算法比较两个矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆