【问题标题】:How can I write entire rows into an Armadillo matrix?如何将整行写入犰狳矩阵?
【发布时间】:2019-04-03 12:20:39
【问题描述】:

在犰狳中,我想将许多行向量保存到一个矩阵中。我可以更改矩阵 A 的条目:

arma:mat A(10,10, 0); // create a 10x10 matrix filled with zeros.
A(i,j) = 1.23; // set element at positon (i,j) to 1.23.

有没有办法一次改变整行矩阵?例如:

arma::rowvec V(10); // a row vector of length 10.
A(i) = V; //write entire rowvector V into matrix at position i.

我知道我可以使用 A.insert_rows(i, V); 将我的向量 V 插入矩阵,但我想替换它。在我的代码中,我已经知道矩阵的尺寸。我也可以在 and 处附加行,但是我在某处读到代码运行得更快,如果我不更改矩阵的大小而是用正确的大小对其进行初始化。 如果有人感兴趣,我正在编写一个求解微分方程的程序,我需要将系统状态(向量)写入数组(犰狳矩阵)以保存时间序列。

我认为我可以使用犰狳矩阵,因为我的系统状态是犰狳向量。我查看了 Armadillo 的文档,但找不到合适的方法。 (正确的意思是写一个循环遍历我的向量的所有条目之外的任何东西 - 我知道 C++ 在内部做这样的事情,但我的猜测是使用犰狳函数会更快)。

【问题讨论】:

    标签: c++ armadillo


    【解决方案1】:

    使用submatrix views。一些例子:

    arma::mat A(10,10, arma::fill::zeros);
    arma::rowvec V(10, arma::fill::randu);
    
    A.row(2) = V;
    A.row(3).ones();
    A.row(4).randu();
    A.row(5).fill(123.4);
    

    【讨论】:

    • 非常感谢!这非常非常有帮助!当我现在想到它时,它实际上是如此明显。我可能没看懂文档……
    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多