简体   繁体   English

Armadillo/C++:如何将向量中的元素分配给立方体管?

[英]Armadillo/C++: How to assign elements from a vector to a cube tube?

Suppose I have a 3x3x4 cube Q (ie a cube having 3 rows, three columns, and 4 slices) and a column vector C with 4 elements (ie as many elements as the slices of Q).假设我有一个 3x3x4 的立方体Q (即一个有 3 行、3 列和 4 个切片的立方体)和一个具有 4 个元素(即与 Q 的切片一样多的元素)的列向量C Is there a way for me to use C to populate a tube of Q?有没有办法让我用 C 填充一管 Q?

I tried the following:我尝试了以下方法:

# include <armadillo>

cube Q(3,3,4);
mat C(4,1, fill::zeros);
Q.tube(0, 0) = C;

but it didn't work (got a runtime exception).但它不起作用(有运行时异常)。 Is there a way to achieve the goal without looping explicitly through the tube and the vector elements?有没有办法在不通过管和向量元素显式循环的情况下实现目标?

SOLVED The code above works just fine.已解决上面的代码工作得很好。 It turns out I was probably doing something else (don't know what) wrong the first time I tried it.事实证明,我第一次尝试时可能做错了其他事情(不知道是什么)。 Thanks to darcamo for pointing out the code actually works!感谢 darcamo 指出代码实际上有效!

That is strange.这很奇怪。 I try it and it worked just fine for me.我试了一下,对我来说效果很好。 Try converting the C matrix into an arma::vec before with the assignment.尝试在赋值之前将C矩阵转换为arma::vec

Q.tube(0, 0) = arma::conv_to<arma::vec>::from(C);

Since a "tube" is one-dimensional it does not make sense to assign a full 2D matrix to it anyway.由于“管”是一维的,因此无论如何为其分配完整的 2D 矩阵是没有意义的。 Although in your case it's a column matrix and it should work.尽管在您的情况下它是一个列矩阵,但它应该可以工作。

I didn't need to convert it, but it is worth trying, since some features in armadillo might not be available due to different flags, compiler support, etc.我不需要转换它,但值得尝试,因为犰狳中的某些功能可能由于不同的标志、编译器支持等而无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM