aKorno
aKorno

Reputation: 21

How to create a binary matrix with rand() and a specific proportion of ones?

I am setting up my mathematical model, but now I am struggling with manipulating the data.

I want to create a random binary matrix (0,1) where the ratio of 1 over the total is equal to some specific value like 0.3. How can I do that with rand() function?

In excel you would do it like the following for the probability of returning 1 is 0.2:

= IF(RAND() < 0.2, 1, 0)

Upvotes: 2

Views: 1018

Answers (1)

JKHA
JKHA

Reputation: 1896

With a comprehension list

This should do the trick, I here suppose that you want a 3x8 Matrix:

function create_random_matrix(n::Int, m::Int)
           [rand() < 0.2 for _ in 1:n, _ in 1:m]
end

mymatrix = create_random_matrix(3,8)

ifelse and ternary operator for a more complicated matrix

You might need ifelse function if you want something more complicated, a matrix of something else than binaries for instance:

function create_random_matrix(n::Int, m::Int)
      [ifelse(rand() < 0.2, 10.0, -5.0+rand()) for _ in 1:n, _ in 1:m]
end

Or a similar way using ternary operator:

function create_random_matrix(n::Int, m::Int)
      [rand() < 0.2 ? 10.0 : -5.0+rand() for _ in 1:n, _ in 1:m]
end

Array of Arrays instead of 2-dimensional Array

I here supposed you want a 2-dimensional Array to represent your matrix. You might want an Array of Arrays instead:

function create_random_matrix(n::Int, m::Int, prob::Float64)
      [[rand() < prob for _ in 1:n] for _ in 1:m]
end

Without putting it in a function

Without putting it in a function:

mymatrix = [[rand() < 0.3 for _ in 1:3] for _ in 1:8]

With a distribution law

You also can, using Distributions package, do the following:

#Using Pkg; Pkg.add("Distributions") #Uncomment if you didn't already install Distributions package
using Distributions
mymatrix = rand(Bernoulli(0.3), 3, 8)

With a bitwise operator

Finally you can also use:

mymatrix = rand(3, 8) .< 0.3

Which is a simple solution notationally and returns a BitArray which may or may not be what you need. This also has the inconvenient to create an unnecessarily array.

Improved thanks to

This answer has been improved thanks to the comments of @Bogumił Kamiński and @DNF.

Upvotes: 8

Related Questions