Ask Your Question
0

create matrix as having a subset of columns from another matrix

asked 2014-09-05 14:52:40 -0500

stablum gravatar image

I need to get a new matrix generated by selecting a subset of columns from another matrix, given a list (or tuple) of column indices.

The following is the code I am working on (there is a bit more than just the attempt to create a new matrix, but might be interesting for you to have some context).

A = matrix(QQ,[
[2,1,4,-1,2],
[1,-1,5,1,1],
[-1,2,-7,0,1],
[2,-1,8,-1,2]
])
print "A\n",A
print "A rref\n",A.rref()
p = A.pivots()
print "A pivots",p

with the following output:

A
[ 2  1  4 -1  2]
[ 1 -1  5  1  1]
[-1  2 -7  0  1]
[ 2 -1  8 -1  2]
A rref
[ 1  0  3  0  0]
[ 0  1 -2  0  0]
[ 0  0  0  1  0]
[ 0  0  0  0  1]
A pivots (0, 1, 3, 4)

Now I expected to find easily a method from matrix objects which allowed to construct a new matrix with a subset of columns by just giving the tuple p as parameter, but could not find anything like that.

Any ideas on how to solve this elegantly in a sage-friendly way? (avoiding for loops and excess code)

thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-09-05 15:27:56 -0500

You can use the matrix_from_columns method: A.matrix_from_columns(p).

edit flag offensive delete link more
1

answered 2014-09-05 14:58:06 -0500

stablum gravatar image

Just found how to do this in the easiest and most concise way:

A[:,p]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-09-05 14:52:40 -0500

Seen: 122 times

Last updated: Sep 05 '14