picchiolu
picchiolu

Reputation: 1130

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

Suppose I have a 3x3x4 cube Q (i.e. a cube having 3 rows, three columns, and 4 slices) and a column vector C with 4 elements (i.e. as many elements as the slices of Q). Is there a way for me to use C to populate a tube of 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!

Upvotes: 0

Views: 1396

Answers (1)

darcamo
darcamo

Reputation: 3493

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.

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. 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.

Upvotes: 1

Related Questions