Generating a random binary matrix with conditions

Hello
I have written the following line of code to generate a random binary matrix
Pop = round(rand(5,5))
It works fine in most cases however sometimes i get columns with all zeros, how do i avoid getting all zeros in any of the column.

 Respuesta aceptada

John D'Errico
John D'Errico el 5 de Ag. de 2016

0 votos

Just throw those columns away, if you get any like that, and replace them with new samples. WTP?

6 comentarios

Correct how do i point to columns which would get zeros and remove them
John D'Errico
John D'Errico el 5 de Ag. de 2016
Editada: John D'Errico el 5 de Ag. de 2016
Basic MATLAB? What does this return?
find(all(X == 0,1))
Or, just regenerate the matrix IF that event happens. Note that it is unlikely. So why bother doing anything special most of the time. Just regenerate the entire array for the unlikely case.
You will get all zeros roughly 1 time in 32 per column that you generate. So this will happen for some column of a 5x5 array roughly 1 time in 6.
any(all(X == 0,1))
Thanks, however the situation demands me not to remove the column, and how do i skip that particular random generation
Well, this is basic matlab:
Pop = randi([0 1], 5);
while any(~any(Pop)) %is there any column with just 0?
Pop = randi([0 1], 5); %regenerate until not
end
Thats true john, but i use this line of code for running in generations upto 500 and in each generation i might use a matrix of 100x100 or even more. So as the matrix size increase i tend to get more number of columns affecting my final output.
Guillaume
Guillaume el 5 de Ag. de 2016
Editada: Guillaume el 5 de Ag. de 2016
But as the size of the matrix increases, the likelihood of getting a whole column of zero decreases (dramatically!), so John answer is even more valid.
The likelyhood of getting a whole column of zero in a 100x100 matrix is 100/2^100 ~ 1 in 1e28. In the very unlikely event that it happens, it takes microseconds to generate a new 100x100 matrix.
>> tic;randi([0 1], 100);toc
Elapsed time is 0.000363 seconds.

Iniciar sesión para comentar.

Más respuestas (1)

A=zeros(5)
[n,m]=size(A)
for k=1:m
id=randperm(n,randi(n))
A(k,id)=1
end

4 comentarios

Hello Azzi thanks for the answer however i need the diagonals to be zero all the time
Rather than introducing new conditions one by one, why don't you tell us the complete list of restrictions on acceptable matrices right now? So far we have:
  1. Cannot contain an all-zero column.
  2. The main diagonal must be all zero.
What else?
Thats pretty much it steven, sorry about that..!!
Are there no restrictions on the number of "true" points in the array? Or is any number of them okay? Do you specifically want 5000 in a 100x100, or do you want an average of 30% of them to be true? Or any other criteria? Whether you realize it or not, you are specifying the fraction of points, at least on average. Like doing round() will say that on average 50% of them will be true, but it doesn't have to be 50% - it can be anything you want it to be.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by