尝试从另一个矩阵类复制时,犰狳会更改整个列而不是单个元素

发布于 2025-01-13 15:54:05 字数 1584 浏览 3 评论 0原文

我正在尝试将矩阵从旧矩阵类复制到犰狳矩阵。不幸的是,当尝试使用 for 循环执行此操作时,当最低元素非零时(我使用稀疏矩阵),犰狳矩阵会将旧矩阵中的值复制到整个列。我已附上下面正在使用的代码。这非常简单,因为我需要首先弄清楚它为什么这样做。根据文档,这应该有效。

main()
{
     OldMatrixClass MatrixA(size, size);
     FillMatrix(MatrixA, size);
     for (auto i = 0; i < size; i++) 
     {
         for (auto j = 0; j < size; j++) 
         {
             file << MatrixA[i][j] << "\t";
         }
         file << "\n";
     }
     arma::Mat<double> ArmaA(size, size);
     ArmaA.zeros();
     CopyMatrix(MatrixA, ArmaA, size);
     for (int i = 0; i < size; i++) 
     {
         for (int j = 0; j < size; j++) 
         {
             file << ArmaA[i,j] << "\t";
         }
         file << "\n";
     }
}

void FillMatrix(OldMatrixClass &A, int size)
{

     double num = 0.15;

     for (int i = 0; i < size; i++)
     {
         for (int j = 0; j < size; j++)
         {
             if (rand() % 101 < 26)
             {
                 A[i][j] = num;
             }
             else
             {
                 A[i][j] = 0;
             }
         }
     }
}

void CopyMatrix(OldMatrixClass A, arma::Mat<double> &B, int size)
{
     for (int k = 0; k < size; k++)
     {
         for (int j = 0; j < size; j++)
         {

             B[j, k] = A[j][k];
         }
     }
}     

这是我运行此代码时的输出...如您所见,如果 MatrixA 列中的最后一个元素非零,则仅更改整个列。
输出

我做错了什么吗?我的语法是否在某个地方混乱了?

I am trying to copy a matrix from an old matrix class to an armadillo matrix. Unfortunately when attempting to do this with a for loop the armadillo matrix will copy the value from the old matrix to the ENTIRE column when the lowest-most element is non-zero (I am using sparse matrices). I have attached the code that is being used below. This is very simplified because I need to figure out why it is doing this first. According to the documentation this should work.

main()
{
     OldMatrixClass MatrixA(size, size);
     FillMatrix(MatrixA, size);
     for (auto i = 0; i < size; i++) 
     {
         for (auto j = 0; j < size; j++) 
         {
             file << MatrixA[i][j] << "\t";
         }
         file << "\n";
     }
     arma::Mat<double> ArmaA(size, size);
     ArmaA.zeros();
     CopyMatrix(MatrixA, ArmaA, size);
     for (int i = 0; i < size; i++) 
     {
         for (int j = 0; j < size; j++) 
         {
             file << ArmaA[i,j] << "\t";
         }
         file << "\n";
     }
}

void FillMatrix(OldMatrixClass &A, int size)
{

     double num = 0.15;

     for (int i = 0; i < size; i++)
     {
         for (int j = 0; j < size; j++)
         {
             if (rand() % 101 < 26)
             {
                 A[i][j] = num;
             }
             else
             {
                 A[i][j] = 0;
             }
         }
     }
}

void CopyMatrix(OldMatrixClass A, arma::Mat<double> &B, int size)
{
     for (int k = 0; k < size; k++)
     {
         for (int j = 0; j < size; j++)
         {

             B[j, k] = A[j][k];
         }
     }
}     

This is the output when I run this code... As you can see only the entire columns are altered if the last element in MatrixA column is non-zero.
Output

Is there something I'm doing wrong? Is my syntax messed up somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2025-01-20 15:54:06

请参阅文档

警告:通过 [i,j] 和 [i,j,k] 访问元素在 C++ 中无法正常工作;而是使用 (i,j) 和 (i,j,k)

See the documentation:

Caveat: accessing elements via [i,j] and [i,j,k] does not work correctly in C++; instead use (i,j) and (i,j,k)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文