This forum has been archived. All content is frozen. Please use KDE Discuss instead.

convert 2 matrices to 1 matrix

Tags: None
(comma "," separated)
ajax20
Registered Member
Posts
10
Karma
0

convert 2 matrices to 1 matrix

Wed Apr 18, 2012 12:12 pm
Hi!

I have got two matrice in the form :

int x,y,z; (x=..,y=..,z=..)

MatrixXd A(x,z) (1,2;5,6;7,8;9,10;......)
MatrixXd B(y,z) (11,1;3,14;11,6;9,0;......)

and i like to combine them to 1 matrix :
MatrixXd AB(x+y,z) (1,2;5,6;7,8;9,10;......;11,1;3,14;11,6;9,0;......)

what is the fastes way? I don't wanna make a new Matrix and copy every single value

Thanks for helping, sorry for my english :)

Greetings
jitseniesen
Registered Member
Posts
204
Karma
2

Re: convert 2 matrices to 1 matrix

Wed Apr 18, 2012 1:25 pm
The easiest way is to use the so-called comma-initializer, for example, "combinedMatrix << matrix1, matrix2;"

See http://eigen.tuxfamily.org/dox-devel/Tu ... ation.html for more details.
ajax20
Registered Member
Posts
10
Karma
0

Re: convert 2 matrices to 1 matrix

Wed Apr 18, 2012 6:38 pm
thank you this is exactly that what I was looking for!

But is it possible to vary the Number of matrices?
if i have an array of matrices like
vector<MatrixXd> test;
and
int o = .. (=numer of combinated matrices)
jitseniesen
Registered Member
Posts
204
Karma
2

Re: convert 2 matrices to 1 matrix

Thu Apr 19, 2012 9:07 am
No, this is not possible with a variable number of matrices. In that case, you best bet is probably to copy the matrices as blocks. For instance (not tested):

Code: Select all
vector<MatrixXd> test;
/* Initialize */
MatrixXd combinedMatrix;
/* Resize combinedMatrix */

int col = 0;
for (vector<MatrixXd>::iterator it = test.begin(); it < test.end(); it++)
{
   combinedMatrix.middleCols(col, it->cols()) = *it;
   col += it->cols();
}

The member function .middleCols(c,n) selects n columns from the matrix starting at column c; see http://eigen.tuxfamily.org/dox/classEig ... 76d363d5d1 .
ajax20
Registered Member
Posts
10
Karma
0

Re: convert 2 matrices to 1 matrix

Sat Apr 21, 2012 5:23 pm
i never had understand iterator's! this is my solution.

Thank you for your help!

Code: Select all
MatrixXd PoU::combinedMatrix(vector<MatrixXd>* A,int von, int bis){
   
   int hilf=0;
   for (int j = von ; j< bis;j++){
      hilf += A->at(j).rows();
   }
   MatrixXd combinedA(hilf,this->A.cols()); //Anfordern des Speichplatz
   int row = 0;
   for (int j = von ; j< bis;j++)
   {
           combinedA.middleRows(row, A->at(j).rows()) = A->at(j);
      row += A->at(j).rows();
   }
   return combinedA;      
}


Greetings Ajax!


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]