Ask Your Question
2

Editing entries of matrix

asked 2011-10-09 16:15:34 +0200

G-Sage gravatar image

updated 2011-10-11 09:40:51 +0200

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 help

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2011-10-09 19:24:19 +0200

DSM gravatar image

I'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?

edit flag offensive delete link more

Comments

You're a genius. I was doing m[2][3] = 99 and it called the error I mentioned above. Thanks for your help.

G-Sage gravatar imageG-Sage ( 2011-10-09 22:07:21 +0200 )edit

To simplify the process of getting help, always cut and paste exactly what you did and the error message rather than paraphrasing it.

DSM gravatar imageDSM ( 2011-10-09 23:50:17 +0200 )edit
1

m[2] returns a vector (which is immutable, IIRC), so m[2][3] is trying to set the entry of an immutable vector.

Jason Grout gravatar imageJason Grout ( 2011-10-10 07:15:15 +0200 )edit

@DSM Yes, a good point, thanks. @Jason Okay, thanks for explaining it.

G-Sage gravatar imageG-Sage ( 2011-10-10 10:37:12 +0200 )edit
0

answered 2011-10-09 16:29:29 +0200

G-Sage gravatar image

updated 2011-10-09 16:34:48 +0200

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?

edit flag offensive delete link more

Comments

Wow, thanks me. That's very helpful. But, not sure if it's a perfect answer.

G-Sage gravatar imageG-Sage ( 2011-10-09 16:29:39 +0200 )edit

@G-Sage No problem, my pleasure. Hopefully someone else can help you out some more.

G-Sage gravatar imageG-Sage ( 2011-10-09 16:29:46 +0200 )edit
1

You can see the 4th column of M with M[:,3] or M.column(3) or M.transpose()[3].

parzan gravatar imageparzan ( 2011-10-09 16:39:38 +0200 )edit

Okay, 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.

G-Sage gravatar imageG-Sage ( 2011-10-09 16:52:03 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2011-10-09 16:15:34 +0200

Seen: 9,699 times

Last updated: Oct 09 '11