1

I am trying to create a 100 by 100 matrix that will be populated with 1's and -1's(at random), and the diagonal of the matrix should be all zeros.

I am new to python and numpy.

Artjom B.
62k26 gold badges136 silver badges236 bronze badges
asked Jun 21, 2013 at 17:23
4
  • 1
    @That1Guy -- Try to be a little more constructive with you comment. Bare "what have you tried" type comments are deprecated Commented Jun 21, 2013 at 17:25
  • @user2509830 -- That's a good start. Can you post the code? We can do a much better job showing you how to use the libraries if we can see how you've chosen to attack the problem. Commented Jun 21, 2013 at 17:27
  • so far, I have created a matrix with all 1's and -1's, but I can't get it to have zeros along the diagonal. Also, when a matrix is created with numpy, how do i access the values( if i wanted to sum up values in a matrix)? Commented Jun 21, 2013 at 17:28
  • In addition to the answers below you should probably read this page: http://wiki.scipy.org/Cookbook/BuildingArrays Commented Jun 21, 2013 at 17:31

3 Answers 3

3

To create the matrix with ones:

a = numpy.ones( (100,100) )

To create the random matrix:

a = numpy.random.random( (100,100) ) # by default between zero and one

To set all the diagonals to zero:

numpy.fill_diagonal(a, 0)
answered Jun 21, 2013 at 17:26
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help. After the matrix is created, how can i access values inside the matrix? for instance, is the value in the 2nd row, 3rd column just example_matrix[2][3]?
essentially, but python uses a 0 to index the first element, so the 2nd row, 3rd column, so it would be example_matrix[1][2]
@user2237635 That is true for python's lists of lists, but a multidimensional numpy array is indexed like [x,y], not [x][y]
Isn't there a numpy.fill_diagonal?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.fill_diagonal.html New in 1.4.0 and likely a lot faster since the indices are never constructed. Just uses fancy slicing.
0

As an alternative, just using list comprehension and random:

from random import randint
x = [1,-1]
my_matrix = [[x[randint(0,1)] if i!=j else 0 for i in range(100)] for j in range(100)]

This will give you the random choice between -1 and 1 with 0 for the diagonal. You can embed this in a function to give you a matrix of NxN size as follows:

from random import randint
def make_matrix(n):
 x = [-1,1]
 return [[x[randint(0,1)] if i!=j else 0 for i in range(n)] for j in range(n)]
answered Jun 21, 2013 at 17:41

Comments

0

Here is a simple python line to create a 2D matrix - 100X100:

 yourmatrix = [[0 for x in xrange(100)] for x in xrange(100)]
Saullo G. P. Castro
59.4k28 gold badges191 silver badges244 bronze badges
answered Jun 21, 2013 at 17:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.