【问题标题】:Armadillo - initialize a matrix and fill matrix with values犰狳 - 初始化矩阵并用值填充矩阵
【发布时间】:2020-06-10 20:40:06
【问题描述】:

我想明智地填充矩阵列。我有以下 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】:

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

    mat m(nrows, nrows); m.zeros();
    
    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多