Control over proportions using randi (or something else )

Hi guys,
I have this line in code that defines a certain number of agents within the selected area
obj.agents = randi([0 1],obj.width, obj.height);
I want to control this parameter so I have a certain proportion of 0 and 1 randomly distributed within the area, like 30% of 0 and 70% of 1, and vice versa (so they still randomly distributed, but one is bigger than the other ).
Are there any relatively easy ways of doing it?

 Accepted Answer

I am not certain what you want to do.
One (relatively simple) approach:
a = 10;
b = 20;
R = randi(10, a, b);
R(R <= 7) = 1;
R(R > 7) = 0;
R_1 = nnz(R)/numel(R)
figure
spy(R)
where ‘R_1’ is the fraction of ‘R’ that are 1. The spy plot is an easy way to show the pattern.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!