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

Matrix copy (only different colounms)

Tags: None
(comma "," separated)
izabelain
Registered Member
Posts
6
Karma
0
Hi all,

I would like to ask that how can I copy different colounms of a matrix pointing by another matrix ?

In Matlab, I do like this,

A = [1 2 3 4; 5 2 1 3; 2 3 4 1]; % 3X4 matrix
ind = [1 3 4]; % 1X3 matrix
B = A(:,ind)
% result of B
B = [1 3 4;5 1 3;2 4 1]
Akryus
Registered Member
Posts
4
Karma
0
OS
You can obtain similar results using this code :

Code: Select all
   MatrixXd A (3,4);

   A << 1, 2, 3, 4,
       5, 2, 1, 3,
       2, 3, 4, 1;

   VectorXi ind(3);

   ind << 0, 2, 3;

   MatrixXd B(A.rows(),ind.size());

   for(int i = 0 ; i < ind.size(); i++)
      B.col(i) = A.col(ind(i));


   cout << B;


I don't know if there is an easiest way to do that.
izabelain
Registered Member
Posts
6
Karma
0
Thanks a lot :)


Bookmarks



Who is online

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