Cody

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

Solution 2259975

Submitted on 6 May 2020
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   Fail
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)

Error using horzcat Dimensions of arrays being concatenated are not consistent. Error in random_binary (line 2) y = [true(1,m) false(n-m)]; y = y(randperm(1:n)); Error in Test1 (line 3) y = random_binary(n,m);

2   Fail
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)

Error using horzcat Dimensions of arrays being concatenated are not consistent. Error in random_binary (line 2) y = [true(1,m) false(n-m)]; y = y(randperm(1:n)); Error in Test2 (line 3) y = random_binary(n,m);

3   Fail
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)

Error using horzcat Dimensions of arrays being concatenated are not consistent. Error in random_binary (line 2) y = [true(1,m) false(n-m)]; y = y(randperm(1:n)); Error in Test3 (line 3) y = random_binary(n,m);