Ask Your Question
4

Add a row / column to a matrix

asked 2015-12-22 14:41:09 +0200

mirgee gravatar image

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!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2015-12-22 17:55:18 +0200

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.

edit flag offensive delete link more

Comments

Note that insert_row method is not implemented for rational matrices.

mirgee gravatar imagemirgee ( 2015-12-28 14:57:53 +0200 )edit
1

There is a feature request to implement this for general matrices here: http://trac.sagemath.org/ticket/15965.

John Palmieri gravatar imageJohn Palmieri ( 2016-01-03 16:49:43 +0200 )edit
1

No progress in 7 years on that issue.

Jaakkonen gravatar imageJaakkonen ( 2021-12-15 10:08:16 +0200 )edit
2

answered 2015-12-22 22:47:43 +0200

ndomes gravatar image
def insert_row(M,k,row):
    return matrix(M.rows()[:k]+[row]+M.rows()[k:])
edit flag offensive delete link more

Comments

With 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.

John Palmieri gravatar imageJohn Palmieri ( 2015-12-23 00:18:01 +0200 )edit

Thanks! This also works on rational and general matrices. To add a column .T can be used for doing transpose, adding a row and then transposing back.

Jaakkonen gravatar imageJaakkonen ( 2021-12-15 10:09:02 +0200 )edit
1

answered 2021-12-15 16:46:50 +0200

slelievre gravatar image

updated 2021-12-15 17:15:29 +0200

The stack and augment methods of matrices provide a workaround.

If a and b have same number of columns, a.stack(b) is

  • the matrix a stacked on top of b
  • equivalently, a with extra rows from b

If a and c have same number of rows, a.augment(c) is

  • the matrix a augmented to the right by c
  • equivalently, a with extra columns from c

For adding rows, one can use thestack method.

sage: a = identity_matrix(3)
sage: b = matrix([[3, 3, 3]])
sage: a[:2, :].stack(b).stack(a[2:, :])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

This works over any ring.

sage: aa = a.change_ring(GF(5))
sage: bb = b.change_ring(GF(5))
sage: aa[:, :2].stack(bb).stack(aa[:, 2:])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

For adding columns, one can similarly use the augment method.

sage: a = identity_matrix(3)
sage: c = matrix([[3], [3], [3]])
sage: a[:, :2].augment(c).augment(a[:, 2:])
[1 0 3 0]
[0 1 3 0]
[0 0 3 1]

This also works over any ring.

sage: aa = a.change_ring(GF(5))
sage: cc = c.change_ring(GF(5))
sage: aa[:, :2].augment(cc).augment(aa[:, 2:])
[1 0 3 0]
[0 1 3 0]
[0 0 3 1]
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: 2015-12-22 14:41:09 +0200

Seen: 12,168 times

Last updated: Dec 15 '21