Cody

Problem 1090. Create a random logical vector of N elements of which M are true.

Solution 1206080

Submitted on 6 Jun 2017 by Said BOUREZG
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
n = 10; m = 4; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.45)<0.2)

ans = 1×10 logical array 0 0 1 1 1 0 1 0 0 0

2   Pass
n = 1000; m = 500; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.45)<0.05)

ans = 1×1000 logical array Columns 1 through 44 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 Columns 45 through 88 1 0 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 1 0 0 1 1 1 1 0 Columns 89 through 132 0 0 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 Columns 133 through 176 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 1 0 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 Columns 177 through 220 1 1 0 0 1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 1 Columns 221 through 264 1 1 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 1 1 1 1 0 1 1 0 1 0 0 0 0 Columns 265 through 308 0 0 1 1 0 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 1 0 Columns 309 through 352 0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 1 Columns 353 through 396 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 1 0 0 0 Columns 397 through 440 1 0 0 1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 0 1 1 1 0 0 1 0 0 0 1 0 1 1 1 1 1 0 1 0 0 Columns 441 through 484 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 Columns 485 through 528 0 1 0 1 1 0 1 1 1 1 1 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 Columns 529 through 572 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 Columns 573 through 616 1 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0 1 0 1 0 Columns 617 through 660 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 1 1 1 1 1 1 1 0 0 1 Columns 661 through 704 0 1 1 0 1 0 0 0 0 1 0 1 0 0 0 1 1 1 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 Columns 705 through 748 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 1 1 0 0 1 0 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 0 1 1 1 Columns 749 through 792 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 Columns 793 through 836 1 0 1 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 Columns 837 through 880 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 0 0 0 1 0 1 1 1 1 0 0 Columns 881 through 924 1 0 1 1 0 1 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 1 Columns 925 through 968 1 0 1 1 1 1 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 1 0 1 1 0 0 0 1 0 1 1 Columns 969 through 1000 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 0 1 0 0 0 1 1

3   Pass
n = 500; m = 20; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.18)<0.05)

ans = 1×500 logical array Columns 1 through 44 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Columns 45 through 88 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 89 through 132 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 133 through 176 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 Columns 177 through 220 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 221 through 264 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 265 through 308 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Columns 309 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 396 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Columns 397 through 440 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Columns 441 through 484 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 485 through 500 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Suggested Problems

More from this Author31

Community Treasure Hunt

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

Start Hunting!