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.Sun, 03 Jan 2016 16:49:43 +0100Add a row / column to a matrixhttps://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/What is the most efficient way to add a row / column to an existing matrix? Are there any handy functions for this? I couldn't find any and any way I can think of seems needlessly complicated... Thanks!Tue, 22 Dec 2015 14:41:09 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/Answer by John Palmieri for <p>What is the most efficient way to add a row / column to an existing matrix? Are there any handy functions for this? I couldn't find any and any way I can think of seems needlessly complicated... Thanks!</p>
https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?answer=31758#post-id-31758 The `insert_row` method does this:
sage: m = identity_matrix(3)
sage: m
[1 0 0]
[0 1 0]
[0 0 1]
sage: m2= m.insert_row(2, [3,3,3])
sage: m2
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
sage: m # original matrix is unchanged
[1 0 0]
[0 1 0]
[0 0 1]
To insert a column, take the transpose, insert a row, and take the transpose again.
Another option:
sage: m = identity_matrix(3)
sage: m.rows()
[(1, 0, 0), (0, 1, 0), (0, 0, 1)]
sage: L = m.rows()
sage: L[2:2] = [(3,3,3)]
sage: L
[(1, 0, 0), (0, 1, 0), (3, 3, 3), (0, 0, 1)]
sage: matrix(L)
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
There is also a `columns` method.
I don't actually know how efficient these procedures are, but you can test them out using `%timeit`, for example.Tue, 22 Dec 2015 17:55:18 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?answer=31758#post-id-31758Comment by mirgee for <p>The <code>insert_row</code> method does this:</p>
<pre><code>sage: m = identity_matrix(3)
sage: m
[1 0 0]
[0 1 0]
[0 0 1]
sage: m2= m.insert_row(2, [3,3,3])
sage: m2
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
sage: m # original matrix is unchanged
[1 0 0]
[0 1 0]
[0 0 1]
</code></pre>
<p>To insert a column, take the transpose, insert a row, and take the transpose again.</p>
<p>Another option:</p>
<pre><code>sage: m = identity_matrix(3)
sage: m.rows()
[(1, 0, 0), (0, 1, 0), (0, 0, 1)]
sage: L = m.rows()
sage: L[2:2] = [(3,3,3)]
sage: L
[(1, 0, 0), (0, 1, 0), (3, 3, 3), (0, 0, 1)]
sage: matrix(L)
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
</code></pre>
<p>There is also a <code>columns</code> method.</p>
<p>I don't actually know how efficient these procedures are, but you can test them out using <code>%timeit</code>, for example.</p>
https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=31885#post-id-31885Note that insert_row method is not implemented for rational matrices.Mon, 28 Dec 2015 14:57:53 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=31885#post-id-31885Comment by John Palmieri for <p>The <code>insert_row</code> method does this:</p>
<pre><code>sage: m = identity_matrix(3)
sage: m
[1 0 0]
[0 1 0]
[0 0 1]
sage: m2= m.insert_row(2, [3,3,3])
sage: m2
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
sage: m # original matrix is unchanged
[1 0 0]
[0 1 0]
[0 0 1]
</code></pre>
<p>To insert a column, take the transpose, insert a row, and take the transpose again.</p>
<p>Another option:</p>
<pre><code>sage: m = identity_matrix(3)
sage: m.rows()
[(1, 0, 0), (0, 1, 0), (0, 0, 1)]
sage: L = m.rows()
sage: L[2:2] = [(3,3,3)]
sage: L
[(1, 0, 0), (0, 1, 0), (3, 3, 3), (0, 0, 1)]
sage: matrix(L)
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
</code></pre>
<p>There is also a <code>columns</code> method.</p>
<p>I don't actually know how efficient these procedures are, but you can test them out using <code>%timeit</code>, for example.</p>
https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=32009#post-id-32009There is a feature request to implement this for general matrices here: http://trac.sagemath.org/ticket/15965.Sun, 03 Jan 2016 16:49:43 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=32009#post-id-32009Answer by ndomes for <p>What is the most efficient way to add a row / column to an existing matrix? Are there any handy functions for this? I couldn't find any and any way I can think of seems needlessly complicated... Thanks!</p>
https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?answer=31765#post-id-31765
def insert_row(M,k,row):
return matrix(M.rows()[:k]+[row]+M.rows()[k:])Tue, 22 Dec 2015 22:47:43 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?answer=31765#post-id-31765Comment by John Palmieri for <pre><code>def insert_row(M,k,row):
return matrix(M.rows()[:k]+[row]+M.rows()[k:])
</code></pre>
https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=31769#post-id-31769With `m=identity_matrix(3)` and `row=[3,3,3]`, running `%timeit m2 = matrix(m.rows()[:2] + [row] + m.rows()[2:])` takes about 40 microseconds, while `%timeit m2 = m.insert_row(2, row)` takes about 3 microseconds.Wed, 23 Dec 2015 00:18:01 +0100https://ask.sagemath.org/question/31754/add-a-row-column-to-a-matrix/?comment=31769#post-id-31769