Armadillo-初始化矩阵并用值填充矩阵

问题描述 投票:0回答:1

我想按矩阵填充矩阵。我有以下numpy代码,我很难转换为C ++ Armadillo。

# numpy code
m = np.zeros((nrows, nrows))
# fill a matrix of lags
for i in range(0, nrows):
    r = np.roll(vec_v, i)
    m[:, i] = r

其中vec_v是单列向量,nrows是该列向量中的行数。

这是我的犰狳尝试

# armadillo conversion
mat m(nrows, nrows); m.zeroes();

for(int i = 0; i < nrows; i++){
  vec r = shift(vec_v, i)
  m.col(i).fill(r);
}

初始化矩阵然后按列填充值的推荐方法是什么。

armadillo
1个回答
1
投票

=运算符应该在这里工作。

mat m(nrows, nrows); m.zeroes();

for(int i = 0; i < nrows; i++){
  vec r = shift(vec_v, i);
  m.col(i) = r;
}

如下所示,可以简化矩阵初始化,并且可以避免生成临时r向量。

mat m(nrows, nrows, fill::zeros);

for(int i = 0; i < nrows; i++){
  m.col(i) = shift(vec_v, i);
}
© www.soinside.com 2019 - 2024. All rights reserved.