将大量矩阵拆分为块
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)
。
相关文章:
- C++将向量的向量拆分为向量的N个子向量
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 使用 C++ 将命令行参数拆分为参数/向量
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何在C++中将数字拆分为数字
- 在线程之间拆分任务总是值得的吗?
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 在 c++ 中拆分类和标头中的继承,错误
- 在矢量内拆分 deques
- 拆分重叠段
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- 通过空行提升拆分字符串
- 给定一个大小为 N 的数组 S,检查是否可以将序列拆分为两个序列
- 如何按多个括号类型拆分字符串,但仍将括号保留在C++?
- 将 Boost.Spirit.X3 解析器拆分为多个 TU
- 将字符串拆分为等长 c++ 的部分
- 将执行循环拆分为多个线程 (1-N-1-N-1..)
- 使用范围拆分string_view
- 将 std::大小为 m 的向量拆分为大小为 n 的向量