2

I am working with largish binary matrices, at the moment up to 100x100.

Lets say I am working with 30x30 binary matrices. Then there are a total of 2^(30x30) binary matrices. I want to select a binary matrix at random, where each of the 2^(30x30) matrices has the same probability of being selected.

My solution attempt was to pick a number between 1 and 2^(30x30) using the function randi(n) with n = 2^(30x30) and then converting the result to the appropriate binary matrix. The problem I ran into was that randi(n) does not take values for n larger than 2^54. Matlab in general does not seem to like very large numbers.

Any suggestions?

2 Answers 2

3

If each matrix of booleans has equal probability, then the elements of the matrix each have equal probability of 0 and 1. You can just fill a matrix of the appropriate size with n² uniform random booleans.

I don't have MATLAB handy, but in Octave you'd do something like unidrnd(2, n, n) - 1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that seems so obvious now. A fresh perspective often helps... :)
3

You can use randint in the range [0 1]:

matrix=randint(30,30,[0 1]);

You can also use rand and threshold the resulting matrix:

matrix=rand(30,30);
matrix=round(matrix);

EDIT: just realized it also works with randi with the following syntax:

matrix=randi([0 1],30,30);

1 Comment

Thanks. I missed the connection that each individual element has equal probability of being either 1 or 0 as @larsmans pointed out.

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.