ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 05 Sep 2014 22:27:56 +0200create matrix as having a subset of columns from another matrixhttps://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/ 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!Fri, 05 Sep 2014 21:52:40 +0200https://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/Answer by John Palmieri for <p>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.</p>
<p>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).</p>
<pre><code>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
</code></pre>
<p>with the following output:</p>
<pre><code>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)
</code></pre>
<p>Now I expected to find easily a method from <code>matrix</code> objects which allowed to construct a new matrix with a subset of columns by just giving the tuple <code>p</code> as parameter, but could not find anything like that.</p>
<p>Any ideas on how to solve this <em>elegantly</em> in a sage-friendly way? (avoiding <code>for</code> loops and excess code)</p>
<p>thanks!</p>
https://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/?answer=24037#post-id-24037 You can use the `matrix_from_columns` method: `A.matrix_from_columns(p)`.Fri, 05 Sep 2014 22:27:56 +0200https://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/?answer=24037#post-id-24037Answer by stablum for <p>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.</p>
<p>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).</p>
<pre><code>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
</code></pre>
<p>with the following output:</p>
<pre><code>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)
</code></pre>
<p>Now I expected to find easily a method from <code>matrix</code> objects which allowed to construct a new matrix with a subset of columns by just giving the tuple <code>p</code> as parameter, but could not find anything like that.</p>
<p>Any ideas on how to solve this <em>elegantly</em> in a sage-friendly way? (avoiding <code>for</code> loops and excess code)</p>
<p>thanks!</p>
https://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/?answer=24036#post-id-24036Just found how to do this in the easiest and most concise way:
A[:,p] Fri, 05 Sep 2014 21:58:06 +0200https://ask.sagemath.org/question/24035/create-matrix-as-having-a-subset-of-columns-from-another-matrix/?answer=24036#post-id-24036