I have four column vectors. I need to append them to make a four by four matrix. Is there a constructor or something for that?
Asked
Active
Viewed 2.1k times
19
-
Possible duplicate of [Initialise Eigen::vector with std::vector](https://stackoverflow.com/questions/17036818/initialise-eigenvector-with-stdvector) – Fantastic Mr Fox Sep 16 '19 at 07:21
-
@FantasticMrFox Eigen has its own matrices and vectors. He is asking about them I guess, not using `std::vector`. – Hakan Demir Oct 24 '21 at 20:03
-
@HakanDemir it was some time ago, but i guess what i was going for was that you could use a vector to initialise it. Anyway, not a dupe. – Fantastic Mr Fox Oct 26 '21 at 06:35
2 Answers
30
You can also append them using the comma initializer syntax:
m << v1, v2, v3, v4;
The matrix m
must have been properly resized first.

ggael
- 28,425
- 2
- 65
- 71
-
3Does this copy the vectors `v1`, `v2`, `v3`, and `v4`? Is it possibly to create the matrix `m` using the data in the vectors without copying them? maybe a const reference or something like that? – ad_ad Nov 18 '17 at 20:12
-
1
-
1
-
11
A quick check on the docs:
Vector4f v1(1,0,0,0);
Vector4f v2(0,1,0,0);
Vector4f v3(0,0,1,0);
Vector4f v4(0,0,0,1);
Matrix4f m;
m.row(0) = v1;
m.row(1) = v2;
m.row(2) = v3;
m.row(3) = v4;
std::cout << m << std::endl;
output:
1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1

Ian Medeiros
- 1,746
- 9
- 24