1

How to use eigen library to compute lower triangular of input matrix without changing columns order?

for example for matrix:

A=[1 2 3;4 5 6 ;7 8 9]

I want the result to be:

1 0 0
4 0 0
7 0 0
abbas
  • 11
  • 1
  • 3
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jun 25 '15 at 07:35
  • What have you tried so far? What do you mean by "without changing columns order"? I don't get what exactly you want to achieve, maybe you can add a minimal example!? – Cleb Jun 25 '15 at 07:36

1 Answers1

7

Your text and your example don't match. I'll go through the three possible ways I understood your question. First, we'll set up the matrix:

Matrix3d mat;
mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;

If you wanted the actual lower triangular matrix, you would use:

std::cout << Matrix3d(mat.triangularView<Lower>()) << "\n\n";

or similar. The result is:

1 0 0
4 5 0
7 8 9

Note the 5,8,9 which are missing from your example. If you just wanted the left-most column, you would use:

std::cout << mat.col(0) << "\n\n";

which gives

1
4
7

If (as the second part of your example shows) you want mat * [1, 0, 0] then you could either do the matrix multiplication (not recommended) or just construct the result:

Matrix3d z = Matrix3d::Zero();
z.col(0) = mat.col(0);

std::cout << z << "\n\n";

which gives the same result as your example:

1 0 0
4 0 0
7 0 0
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56