将大量矩阵拆分为块

Splitting a massive matrix into blocks

本文关键字:拆分      更新时间:2023-10-16

>我有一个问题。我正在做一个任务,试图在另一个矩阵(向量)中找到一个矩阵(向量),矩阵的大小是:

海量矩阵:1024x768 小矩阵:36x49

基本上,我的理论是将大矩阵分成小矩阵大小的块,这意味着我能够只看到小矩阵是否存在于哪个块中,然后输出块。但是,它不会平均拆分,但我需要一种方法来确定小矩阵是否确实存在于大矩阵中。

例如,我将使用测试数据:

M1 = 
    0 1 0 0 
    1 1 1 1
    0 0 0 0
    1 0 1 1
M2 = 
    0 1
    1 1
然后

我会将矩阵分成 2x2 的块,然后以这种方式进行检查。这很简单,因为我只使用一个小矩阵,并且矩阵可以平均拆分,而上面的问题要复杂得多,难以理解和解决。

从本质上讲,我需要能够将 (1024x768) 拆分为 (36x49) 的块大小,以便我可以进行检查以确定该特定矩阵的位置。我一直在使用这个算法:

// Assume: 
// matrix1ColSize = 768
// matrix2ColSize = 49
const int ROW_BOUNDS = matrix1.size() - matrix2.size();
const int COL_BOUNDS = matrix1ColSize - matrix2ColSize;
bool found = false;
for(int i=0; (i < ROW_BOUNDS); i++)
{
    bool matchFound = false;
    for(int j=0; (j < COL_BOUNDS); j++) {
        // logic here
    }
    cout << endl;
}

有人可以提供任何建议吗?这真的让我很烦:(!

如果两个矩阵的所有元素都相同,则它们是相同的。因此,以下伪代码将小矩阵与大矩阵中的块进行比较:

Initialize result to "true"
For each position in the small matrix
    Read the value from the large matrix; call it x1
    Read the value from the small matrix; call it x2
    If x1 is not equal to x2, set result to "false"
    (Optional) If x1 is not equal to x2, stop looking at other positions
Here, use the result

这个逻辑将位于您的 2 个嵌套循环中,因此您将在那里拥有 4 个嵌套循环!如果您担心感到困惑,请将实现放在函数中。如果你想使用4个嵌套循环,祝你好运。

在 C++ 中:

bool is_equal = true;
for (int y = 0; y < 49; ++y)
{
    for (int x = 0; x < 36; ++x)
    {
        if (matrix1.at(j + x, i + y) != matrix2.at(x, y))
        {
            is_equal = false;
            goto DONE; // optional
        }
    }
}
DONE:;

编辑:此代码假设对矩阵使用自定义类;再次查看您的代码后,我意识到您可能使用向量向量(std::vector<std::vector<int>>),因此请使用matrix2[y][x]而不是matrix2.at(x, y)