Ask Your Question
0

create matrix as having a subset of columns from another matrix

asked 2014-09-05 21:52:40 +0100

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 22:27:56 +0100

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

edit flag offensive delete link more
1

answered 2014-09-05 21:58:06 +0100

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 21:52:40 +0100

Seen: 1,319 times

Last updated: Sep 05 '14