Generating a random binary matrix with conditions
Mostrar comentarios más antiguos
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
Más respuestas (1)
Azzi Abdelmalek
el 5 de Ag. de 2016
A=zeros(5)
[n,m]=size(A)
for k=1:m
id=randperm(n,randi(n))
A(k,id)=1
end
4 comentarios
Christy Jackson
el 5 de Ag. de 2016
Steven Lord
el 5 de Ag. de 2016
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:
- Cannot contain an all-zero column.
- The main diagonal must be all zero.
What else?
Christy Jackson
el 5 de Ag. de 2016
Image Analyst
el 5 de Ag. de 2016
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.
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!