Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将矩阵拆分为小矩阵块的方法_C++_Algorithm_Math - Fatal编程技术网

C++ 将矩阵拆分为小矩阵块的方法

C++ 将矩阵拆分为小矩阵块的方法,c++,algorithm,math,C++,Algorithm,Math,我有一个问题,想知道是否有人能提供一个理想的解决方案 基本上(小数据),但是,如果我有这样一个矩阵: 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 然后我需要将该矩阵拆分为与第二个矩阵大小相同的块,在本例中为2x2: 0 1 1 1 我知道它与偏移量x/Y值有关,这些值随(小)矩阵的大小而变化,我只是不知道计算这些结果的计算方法。我将把偏移量x/Y传递给一个函数(带向量),这样我可以计算特定块的和 有人有什么建议吗 谢谢导入标准std

我有一个问题,想知道是否有人能提供一个理想的解决方案

基本上(小数据),但是,如果我有这样一个矩阵:

     0 1 0 0
     1 1 1 0
     0 0 0 0
     1 1 0 0
然后我需要将该矩阵拆分为与第二个矩阵大小相同的块,在本例中为2x2:

0 1
1 1
我知道它与偏移量x/Y值有关,这些值随(小)矩阵的大小而变化,我只是不知道计算这些结果的计算方法。我将把偏移量x/Y传递给一个函数(带向量),这样我可以计算特定块的和

有人有什么建议吗

谢谢

导入标准stdio:writeln;
void main(字符串[]args)
{
int m=4,n=4;
int[][]矩阵=[[0,1,0,0],[1,1,1,0],[0,0,0,0],[1,1,0,0];
int mm=2,nn=2;
整数和;

对于(int x=0;x为了原位拆分矩阵(即,不复制它),您需要一种既能处理原始片段又能处理拆分片段的表示法——这将使定义递归算法变得更容易,等等:

template <class elt> class MatRef {
    elt* m_data;
    unsigned m_rows, m_cols;
    unsigned m_step;

    unsigned index(unsigned row, unsigned col)
    {
        assert(row < m_rows && col < m_cols);
        return m_step * row + col;
    }

public: // access
    elt& operator() (unsigned row, unsigned col)
    {
        return m_data[index(row,col)];
    }

public: // constructors
    MatRef(unsigned rows, unsigned cols, elt* backing)  // original matrix
        : m_rows(rows)
        , m_cols(cols)
        , m_step(cols)
        , m_data(backing)
    {}
    MatRef(unsigned start_row, unsigned start_col,
           unsigned rows, unsigned cols, MatRef &orig) // sub-matrix
        : m_rows(rows)
        , m_cols(cols)
        , m_step(orig.m_step)
        , m_data(orig.m_data + orig.index(start_row, start_col))
    {
        assert(start_row+rows <= orig.m_rows && start_col+cols <= orig.m_cols);
    }
};
模板类MatRef{
elt*m_数据;
未签名的m_行、m_列;
无符号m_步;
无符号索引(无符号行、无符号列)
{
断言(行<行和列<列);
返回m_步*行+列;
}
公共访问
elt运算符()(无符号行、无符号列)
{
返回m_数据[索引(行、列)];
}
公共构造函数
MatRef(无符号行、无符号列、elt*支持)//原始矩阵
:m_行(行)
,m_cols(cols)
,m_阶(cols)
,m_数据(支持)
{}
MatRef(无符号起始行、无符号起始列、,
无符号行、无符号列、MatRef&orig)//子矩阵
:m_行(行)
,m_cols(cols)
,m_步(原始m_步)
,m_数据(原始m_数据+原始索引(起始行,起始列))
{

assert(start_row+rows从数学上讲,您可以使用曲线(例如z曲线或peano曲线)拆分矩阵。这样还可以降低维数复杂性。z曲线使用4个四元来拆分平面,并与四元树相似


编辑:我刚刚了解到这是z阶曲线,而不是z阶曲线:。z阶曲线在生物信息学中是3d的东西???哈哈!我不是生物信息学家,也不是维基百科的,但这听起来像胡说八道。z阶曲线也可以覆盖3d区域。也许维基百科想这么说?这里是3d z阶曲线的大图。它甚至在wi上kipedia文章????

如果它是5x5矩阵,你想怎么做?嘿-它仍然是1D矩阵,所以它只能分成与小矩阵大小相同的块,在这种情况下,分成2x2矩阵..我想的是这样的:const int ROW_BOUNDS=matrix1.size()-matrix2.size();const int COL_BOUNDS=matrix1.size()-matrix2.size();如果这有意义的话?我的意思是,你怎么处理铰列和铰线?我不完全理解你的问题应该有什么困难。你是说你想尝试所有可能的子矩阵大小?还是说你的初始矩阵存储为1D数组,这就是为什么你不知道如何进行边界检查。然后把一些ng与伪代码有点像,而不仅仅是“你可能想象得到的最可怕的东西,比如C”。这与伪代码有点像。D实际上是一种非常有趣的语言。将导入更改为#include,切换主声明,更改writeln(x)对std::cout-oh,以及求和的第一个参数的类型。嗯…什么?这些与数组切片操作有什么关系?@Li aung-Yip:你是在建议我喝啤酒吗?这个“步骤”有时被称为“跨步”.此外,BLAS和LAPACK也理解这种表示法,如果您计划做非平凡线性代数,这可能会很有用。
template <class elt> class MatRef {
    elt* m_data;
    unsigned m_rows, m_cols;
    unsigned m_step;

    unsigned index(unsigned row, unsigned col)
    {
        assert(row < m_rows && col < m_cols);
        return m_step * row + col;
    }

public: // access
    elt& operator() (unsigned row, unsigned col)
    {
        return m_data[index(row,col)];
    }

public: // constructors
    MatRef(unsigned rows, unsigned cols, elt* backing)  // original matrix
        : m_rows(rows)
        , m_cols(cols)
        , m_step(cols)
        , m_data(backing)
    {}
    MatRef(unsigned start_row, unsigned start_col,
           unsigned rows, unsigned cols, MatRef &orig) // sub-matrix
        : m_rows(rows)
        , m_cols(cols)
        , m_step(orig.m_step)
        , m_data(orig.m_data + orig.index(start_row, start_col))
    {
        assert(start_row+rows <= orig.m_rows && start_col+cols <= orig.m_cols);
    }
};