5

I need to create an 2D array.

    import numpy as np
    self.col = 10
    self.row = 5

    ...

    matrix = np.array(self.row, self.col) # NOT WORKING

What is the right syntax please

i also need to fill it with random binary data

2 Answers 2

10

Generate a random matrix with binary values:

import numpy as np
row, col = 10, 5
matrix = np.random.randint(2, size=(row,col))
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np

def toBit(x):
        if x<= 0:
            x = 0               
        else:
            x = 1
        return x

VtoBit = np.vectorize(toBit)

arr1 = np.random.randn(6,10)
arr2 = VtoBit(arr1)

print(arr2)

1 Comment

What is the benefit, if any, of your approach to that specified in the accepted answer for seven years ago?

Your Answer

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.