Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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+中内存有效的列和替换(m×m)矩阵中对角线的值+;_C++_Matrix - Fatal编程技术网

C++ 用C+中内存有效的列和替换(m×m)矩阵中对角线的值+;

C++ 用C+中内存有效的列和替换(m×m)矩阵中对角线的值+;,c++,matrix,C++,Matrix,我有一个大小为m=4的矩阵 0.00000 0.09130 0.09130 0.00000 0.04565 0.00000 0.00000 0.00000 0.04565 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 我想替换矩阵的对角线 带(1-其列的和)。结果矩阵: 0.90870 0.09130 0.09130 0.00000

我有一个大小为m=4的矩阵

   0.00000   0.09130   0.09130   0.00000
   0.04565   0.00000   0.00000   0.00000
   0.04565   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000   0.00000
我想替换矩阵的对角线 带(1-其列的和)。结果矩阵:

   0.90870   0.09130   0.09130   0.00000
   0.04565   0.90870   0.00000   0.00000
   0.04565   0.00000   0.90870   0.00000
   0.00000   0.00000   0.00000   1.00000
例如,对于(1,1),我们有

   1 - (0.04565 + 0.04565 + 0.00000) = 0.90870
现在实际操作中,
m
的大小非常大 比例从10^6到10^7。所以我没钱存储初始矩阵 放入容器中

有没有节省内存的替代方法

当前的实现就是我对它的slurping 转化为向量的向量。它不能处理大的m(10^6)

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//在Slurping之前初始化矩阵
向量矩阵;
矩阵。调整大小(nofRow);
对于(尺寸i=0;i否则{cout创建一个大小为m的向量来存储对角线。然后遍历该文件并将每行的第i列添加到diag[i]。现在再次遍历该文件并输出每行,但将第i行的第i个元素的值替换为diag[i]。这样,您只需要在内存中存储一个大小为m的向量。

创建一个大小为m的向量来存储对角线。然后遍历该文件并将每行的第i列添加到diag[i]。现在再次遍历该文件并输出每行,但将第i行的第i个元素的值替换为diag[i]。这样,您只需要在内存中存储一个大小为m的向量。

此外,假设磁盘I/O将成为瓶颈,并且1PB的输入数据物理存储在多个磁盘卷上,则需要某种并行化。表示对角线的向量可以很容易地分片,因此此算法有望成为一种好的算法开始。此外,假设磁盘I/O将成为瓶颈,并且1PB的输入数据物理存储在多个磁盘卷上,则需要某种并行化。表示对角线的向量可以很容易地分割,因此该算法有望成为一个良好的开端。
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <map>
    using namespace std;

      // Initialize Matrix Before Slurping
       vector <vector<double> > Matrix;
        Matrix.resize(nofRow);
        for(size_t i = 0; i < nofRow; ++i)
        {
            Matrix[i].resize(nofCol);
        }



        if (arg_count !=2 ) {
        cerr << "expected one argument" << endl;
        return EXIT_FAILURE;
    }

    string line;
    ifstream myfile (arg_vec[1]);

    // Slurpint it
    int count1=0;
    if (myfile.is_open())
    {   

        while (getline(myfile,line) )
        {
            stringstream ss(line);
            double Value;
            count1++;            

            int count2=0;
            while (ss >> Value) {
                count2++;
                Matrix[count1][count2] = Value;
            }


        }
        myfile.close();
    }
    else { cout << "Unable to open file"; }


     // Summing up Column;
        vector <double> ColSum;
        ColSum.resize(nofCol);
        for(size_t i = 0; i < nofRow; ++i)
        {
            for(size_t j = 0; j < nofCol; ++j)
            {
                //std::cout <<"["<<i<<"]"<<"["<<j<<"] = " <<Matrix[i][j]<<std::endl;
                ColSum[j] += Matrix[i][j];
            }
        }  



        // Printing it
        for(size_t k = 0; k < nofRow; ++k)
        {
            for(size_t l = 0; l < nofCol; ++l)
            {
                  if (k == l ) {
                      double OneMinusSum = 1 - ColSum[k];
                      //if (OneMinusSum < 0) { OneMinusSum = 1; };
                     std::cout << OneMinusSum << "\t";
                  }
                  else {
                      std::cout<< Matrix[k][l] << "\t";
                  }
            }

            std::cout << std::endl;
        }