ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 10 Oct 2011 03:37:12 -0500Editing entries of matrixhttp://ask.sagemath.org/question/8366/editing-entries-of-matrix/I'm looking at a matrix, say adjacency matrix of a graph, g.am(). I want to change some of the entries now. I am told it's a vector and it's immutable. What can I do?
One thought I had is adding some other matrix to it with only one nonblank entry. But, is there some easy way to do that?
Another thought I had was turning it into a list, list(g.am()). Maybe that's acceptable. I'm not 100% sure yet for what I'm working on. If I go that way, is there a way to print it in a matrix form again, i.e., entries print in a square shape.
Thanks for any helpSun, 09 Oct 2011 09:15:34 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/Answer by DSM for <p>I'm looking at a matrix, say adjacency matrix of a graph, g.am(). I want to change some of the entries now. I am told it's a vector and it's immutable. What can I do?</p>
<p>One thought I had is adding some other matrix to it with only one nonblank entry. But, is there some easy way to do that?</p>
<p>Another thought I had was turning it into a list, list(g.am()). Maybe that's acceptable. I'm not 100% sure yet for what I'm working on. If I go that way, is there a way to print it in a matrix form again, i.e., entries print in a square shape.</p>
<p>Thanks for any help</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?answer=12736#post-id-12736I'm confused. At least in Sage 4.7.1, the adjacency matrix returned by .am() isn't immutable.
sage: G = graphs.DiamondGraph()
sage: m = G.am()
sage: m[2,3] = 99
sage: m
[ 0 1 1 0]
[ 1 0 1 1]
[ 1 1 0 99]
[ 0 1 1 0]
sage: m[:,1] = 17
sage: m
[ 0 17 1 0]
[ 1 17 1 1]
[ 1 17 0 99]
[ 0 17 1 0]
sage: m[:,0] = vector([6,5,4,3])
sage: m
[ 6 17 1 0]
[ 5 17 1 1]
[ 4 17 0 99]
[ 3 17 1 0]
Even assuming it were immutable, making a new copy of it would be a natural thing to do. Could you cut-and-paste exactly what you did and the error message it generated?Sun, 09 Oct 2011 12:24:19 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?answer=12736#post-id-12736Comment by DSM for <p>I'm confused. At least in Sage 4.7.1, the adjacency matrix returned by .am() isn't immutable.</p>
<pre><code>sage: G = graphs.DiamondGraph()
sage: m = G.am()
sage: m[2,3] = 99
sage: m
[ 0 1 1 0]
[ 1 0 1 1]
[ 1 1 0 99]
[ 0 1 1 0]
sage: m[:,1] = 17
sage: m
[ 0 17 1 0]
[ 1 17 1 1]
[ 1 17 0 99]
[ 0 17 1 0]
sage: m[:,0] = vector([6,5,4,3])
sage: m
[ 6 17 1 0]
[ 5 17 1 1]
[ 4 17 0 99]
[ 3 17 1 0]
</code></pre>
<p>Even assuming it were immutable, making a new copy of it would be a natural thing to do. Could you cut-and-paste exactly what you did and the error message it generated?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21150#post-id-21150To simplify the process of getting help, always cut and paste exactly what you did and the error message rather than paraphrasing it.Sun, 09 Oct 2011 16:50:17 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21150#post-id-21150Comment by G-Sage for <p>I'm confused. At least in Sage 4.7.1, the adjacency matrix returned by .am() isn't immutable.</p>
<pre><code>sage: G = graphs.DiamondGraph()
sage: m = G.am()
sage: m[2,3] = 99
sage: m
[ 0 1 1 0]
[ 1 0 1 1]
[ 1 1 0 99]
[ 0 1 1 0]
sage: m[:,1] = 17
sage: m
[ 0 17 1 0]
[ 1 17 1 1]
[ 1 17 0 99]
[ 0 17 1 0]
sage: m[:,0] = vector([6,5,4,3])
sage: m
[ 6 17 1 0]
[ 5 17 1 1]
[ 4 17 0 99]
[ 3 17 1 0]
</code></pre>
<p>Even assuming it were immutable, making a new copy of it would be a natural thing to do. Could you cut-and-paste exactly what you did and the error message it generated?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21145#post-id-21145@DSM Yes, a good point, thanks. @Jason Okay, thanks for explaining it.Mon, 10 Oct 2011 03:37:12 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21145#post-id-21145Comment by G-Sage for <p>I'm confused. At least in Sage 4.7.1, the adjacency matrix returned by .am() isn't immutable.</p>
<pre><code>sage: G = graphs.DiamondGraph()
sage: m = G.am()
sage: m[2,3] = 99
sage: m
[ 0 1 1 0]
[ 1 0 1 1]
[ 1 1 0 99]
[ 0 1 1 0]
sage: m[:,1] = 17
sage: m
[ 0 17 1 0]
[ 1 17 1 1]
[ 1 17 0 99]
[ 0 17 1 0]
sage: m[:,0] = vector([6,5,4,3])
sage: m
[ 6 17 1 0]
[ 5 17 1 1]
[ 4 17 0 99]
[ 3 17 1 0]
</code></pre>
<p>Even assuming it were immutable, making a new copy of it would be a natural thing to do. Could you cut-and-paste exactly what you did and the error message it generated?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21151#post-id-21151You're a genius. I was doing m[2][3] = 99 and it called the error I mentioned above. Thanks for your help.Sun, 09 Oct 2011 15:07:21 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21151#post-id-21151Comment by Jason Grout for <p>I'm confused. At least in Sage 4.7.1, the adjacency matrix returned by .am() isn't immutable.</p>
<pre><code>sage: G = graphs.DiamondGraph()
sage: m = G.am()
sage: m[2,3] = 99
sage: m
[ 0 1 1 0]
[ 1 0 1 1]
[ 1 1 0 99]
[ 0 1 1 0]
sage: m[:,1] = 17
sage: m
[ 0 17 1 0]
[ 1 17 1 1]
[ 1 17 0 99]
[ 0 17 1 0]
sage: m[:,0] = vector([6,5,4,3])
sage: m
[ 6 17 1 0]
[ 5 17 1 1]
[ 4 17 0 99]
[ 3 17 1 0]
</code></pre>
<p>Even assuming it were immutable, making a new copy of it would be a natural thing to do. Could you cut-and-paste exactly what you did and the error message it generated?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21147#post-id-21147m[2] returns a vector (which is immutable, IIRC), so m[2][3] is trying to set the entry of an immutable vector.Mon, 10 Oct 2011 00:15:15 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21147#post-id-21147Answer by G-Sage for <p>I'm looking at a matrix, say adjacency matrix of a graph, g.am(). I want to change some of the entries now. I am told it's a vector and it's immutable. What can I do?</p>
<p>One thought I had is adding some other matrix to it with only one nonblank entry. But, is there some easy way to do that?</p>
<p>Another thought I had was turning it into a list, list(g.am()). Maybe that's acceptable. I'm not 100% sure yet for what I'm working on. If I go that way, is there a way to print it in a matrix form again, i.e., entries print in a square shape.</p>
<p>Thanks for any help</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?answer=12735#post-id-12735To answer myself, matrix(whatever_the_name_of_the_list_that_came_from_the_original_matrix_is) works. This might be all I need. But, this still isn't perfect, because I'm wanting to look at columns of the matrix and I don't know a good way to do that with lists... matrix[a] gives the row a... well okay, I can just make a new list and do a for loop that loops through the rows and picks of the ath entries in each row and appends it to the list, which would give me the ath column. But, that's not as easy as matrix.column(a). Is there a way with lists other than what I just said?Sun, 09 Oct 2011 09:29:29 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?answer=12735#post-id-12735Comment by G-Sage for <p>To answer myself, matrix(whatever_the_name_of_the_list_that_came_from_the_original_matrix_is) works. This might be all I need. But, this still isn't perfect, because I'm wanting to look at columns of the matrix and I don't know a good way to do that with lists... matrix[a] gives the row a... well okay, I can just make a new list and do a for loop that loops through the rows and picks of the ath entries in each row and appends it to the list, which would give me the ath column. But, that's not as easy as matrix.column(a). Is there a way with lists other than what I just said?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21154#post-id-21154@G-Sage No problem, my pleasure. Hopefully someone else can help you out some more.Sun, 09 Oct 2011 09:29:46 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21154#post-id-21154Comment by G-Sage for <p>To answer myself, matrix(whatever_the_name_of_the_list_that_came_from_the_original_matrix_is) works. This might be all I need. But, this still isn't perfect, because I'm wanting to look at columns of the matrix and I don't know a good way to do that with lists... matrix[a] gives the row a... well okay, I can just make a new list and do a for loop that loops through the rows and picks of the ath entries in each row and appends it to the list, which would give me the ath column. But, that's not as easy as matrix.column(a). Is there a way with lists other than what I just said?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21155#post-id-21155Wow, thanks me. That's very helpful. But, not sure if it's a perfect answer.Sun, 09 Oct 2011 09:29:39 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21155#post-id-21155Comment by parzan for <p>To answer myself, matrix(whatever_the_name_of_the_list_that_came_from_the_original_matrix_is) works. This might be all I need. But, this still isn't perfect, because I'm wanting to look at columns of the matrix and I don't know a good way to do that with lists... matrix[a] gives the row a... well okay, I can just make a new list and do a for loop that loops through the rows and picks of the ath entries in each row and appends it to the list, which would give me the ath column. But, that's not as easy as matrix.column(a). Is there a way with lists other than what I just said?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21153#post-id-21153You can see the 4th column of M with M[:,3] or M.column(3) or M.transpose()[3].Sun, 09 Oct 2011 09:39:38 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21153#post-id-21153Comment by G-Sage for <p>To answer myself, matrix(whatever_the_name_of_the_list_that_came_from_the_original_matrix_is) works. This might be all I need. But, this still isn't perfect, because I'm wanting to look at columns of the matrix and I don't know a good way to do that with lists... matrix[a] gives the row a... well okay, I can just make a new list and do a for loop that loops through the rows and picks of the ath entries in each row and appends it to the list, which would give me the ath column. But, that's not as easy as matrix.column(a). Is there a way with lists other than what I just said?</p>
http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21152#post-id-21152Okay, now that's what I'm talking about. Actually, the last two options don't work because I'm taking a matrix and making it into a list (so I can edit it). But, the first one you "list"ed does work if you do M[:][3]. So, thanks very much.Sun, 09 Oct 2011 09:52:03 -0500http://ask.sagemath.org/question/8366/editing-entries-of-matrix/?comment=21152#post-id-21152