19

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?

DanielLC
  • 5,801
  • 5
  • 18
  • 16
  • 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 Answers2

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