我想按矩阵填充矩阵。我有以下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);
}
初始化矩阵然后按列填充值的推荐方法是什么。
=
运算符应该在这里工作。
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);
}