Editing Large Matrix Python

时间:2016-07-11 19:48:36

标签: python matrix

I want to make a 34x34 Matrix consisting of entirely zeroes and ones. I have an array that lists the coordinates where all of the ones should go but don't know how to use it. The array looks like this:

0 1 1

0 2 1

0 3 1

1 1 1

where the first number in each row is the x coordinate, the second number in each row is the y coordinate, and the third number is the desired value (always 1).

I tried to create a blank matrix using Matrix=numpy.zeros(34,34) but I don't know how to change the desired values all at once.

Any idea how to take a matrix and change multiple values at once?

1 个答案:

答案 0 :(得分:0)

That's work:

a = np.array([[0,1,1],[0,2,1],[0,3,1],[1,1,1]])
m = np.zeros([5,5])
for i in range(len(a)):
   m[a[i][0],a[i][1]] = a[i][2]  # Or = 1 if that's always the case

And the m matrix is:

array([[ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])