Reputation: 21
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
Reputation: 1896
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)
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
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:
mymatrix = [[rand() < 0.3 for _ in 1:3] for _ in 1:8]
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)
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.
This answer has been improved thanks to the comments of @Bogumił Kamiński and @DNF.
Upvotes: 8