On Mon, May 4, 2009 at 5:37 PM, Barry Dewitt <
barry...@gmail.com> wrote:
> Hi Dr. Stein,
>
> I wrote the following code that is meant to create a random nxn symmetric
> matrix with 0's along the diagonal:
>
> @interact
> def _(n = 2):
> m = matrix(ZZ, n, n, [0 for i in [0..n-1] for j in [0..n-1]])
> for i in [0..n-1]:
> for j in [i+1..n-1]:
> m[i][j] = randint(0,1)
> for i in [0..n-1]:
> for j in [0..i]:
> m[i][j] = m[j][i]
> show(m)
>
> The first two for loops set the diagonal to zero and everything above the
> diagonal to a zero or one; the second set of for loops is meant to make the
> matrix symmetric. I keep getting the following error: "ValueError: vector
> is immutable; please change a copy instead (use self.copy())" All the
> documentation I've been able to find says that the matrix is mutable, so I'm
> confused why I can't change the entries of the matrix after I create it. I
> also tried playing around with m.copy() as the error message suggests, but
> have had no success. I was wondering if you could point me in the right
> direction, because I think that my approach makes sense (to me, anyway), and
> I want to figure out how I can make it work!
Use m[i,j] not m[i][j].
William