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

Selection of matrix elements based on a boolean vector

Tags: None
(comma "," separated)
alesstimec
Registered Member
Posts
3
Karma
0
OS
Hello...

Is there any way to select elements of a matrix/vector in the way it is done in matlab: B = A( I ); where I is a boolean vector and the result, B, is composed of only those elements of A for which the corresponding elements of I are true...

So.. is there any way to do this in Eigen? Likewise i would like to select only those rows/columns of a matrix for which the corresponding elements of a boolean vector are true...

Example:
I = [1 0 1];
I1 [1 1 0 0 1 0 0 0 1]
A = [[1 2 3],[4 5 6],[7 8 9]]

i'd like to do something like:
- A.selectRows( I ) and get [[1 2 3], [7 8 9]]
- A.selectColumns( I ) and get [[1 7], [2 8], [3 9]]
- A.selectElements( I1 ) and get a vector [1 4 5 9]


Cheers,
Ales
alesstimec
Registered Member
Posts
3
Karma
0
OS
Oh.. forgot to mention it..

In matlab one can also do B = A([1 1 2 3],:); This will result in B which is almost the same as A except that row 1 is now repeated twice...

Example:

Slovenia
Selection of matrix elements based on a boolean vector

Postby alesstimec » Fri Sep 18, 2009 12:00 pm

Hello...

Is there any way to select elements of a matrix/vector in the way it is done in matlab: B = A( I ); where I is a boolean vector and the result, B, is composed of only those elements of A for which the corresponding elements of I are true...

So.. is there any way to do this in Eigen? Likewise i would like to select only those rows/columns of a matrix for which the corresponding elements of a boolean vector are true...

Example 1:
A = [[1 2 3],[4 5 6],[7 8 9]]

and if you do:
B = A( [1 1 2 3 2], : );

then
B = [[1 2 3],[1 2 3],[4 5 6],[7 8 9],[4 5 6]]

Example 2:
A = [[1 2 3],[4 5 6],[7 8 9]]

and if you do:
B = A( :, [1 1 2 3] );

then
B = [[1 1 2 3], [4 4 5 6], [7 7 8 9]]

If this could be done in Eigen then i'd be in HEAVEN!!!

Cheers,
Ales
User avatar
bjacob
Registered Member
Posts
658
Karma
3
alesstimec wrote:Hello...

Is there any way to select elements of a matrix/vector in the way it is done in matlab: B = A( I ); where I is a boolean vector and the result, B, is composed of only those elements of A for which the corresponding elements of I are true...


No, there's no way to do that at the moment.

This would inherently be very slow as an expression. Every coeff read would require to traverse the array of bools. So it would only make sense in practice as a function returning a new matrix.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
bjacob
Registered Member
Posts
658
Karma
3
alesstimec wrote:In matlab one can also do B = A([1 1 2 3],:); This will result in B which is almost the same as A except that row 1 is now repeated twice...


Ah, that makes a lot more sense :) We don't have this either in Eigen but already discussed that that would be nice to have. Feel free to contribute the patch, and anyway the next major version will likely have this.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
alesstimec
Registered Member
Posts
3
Karma
0
OS
Hey..

yes, i already have functions doing that. I just thought there might be a function foing that..


But if the next major version will have some of that then YEEEEEYYY!!! can't wait :D

Thnx,
Ales


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]