Cody

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

Solution 491092

Submitted on 26 Aug 2014 by Abdullah Caliskan
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)

k = 6 7 8 4 2 9 10 3 5 1 M = 0 0 0 0 0 0 0 0 0 0 M = 0 0 0 1 0 1 1 1 0 0 ans = 0 0 0 1 0 1 1 1 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)

k = Columns 1 through 8 440 631 621 908 911 196 81 722 Columns 9 through 16 538 82 120 230 364 482 365 113 Columns 17 through 24 991 641 808 552 447 176 62 936 Columns 25 through 32 586 164 886 340 803 231 580 514 Columns 33 through 40 619 959 690 483 502 424 468 521 Columns 41 through 48 954 218 497 948 786 163 84 577 Columns 49 through 56 300 604 871 620 980 995 611 574 Columns 57 through 64 400 156 565 534 595 931 964 5 Columns 65 through 72 128 874 732 152 281 686 885 222 Columns 73 through 80 740 492 727 992 142 455 979 366 Columns 81 through 88 767 9 257 286 118 166 795 792 Columns 89 through 96 256 89 993 421 640 332 403 392 Columns 97 through 104 210 668 636 490 793 708 775 493 Columns 105 through 112 869 110 323 477 65 423 298 325 Columns 113 through 120 450 703 407 977 167 399 111 42 Columns 121 through 128 314 764 99 715 466 693 16 830 Columns 129 through 136 98 872 273 848 966 64 758 202 Columns 137 through 144 648 971 324 261 96 625 796 956 Columns 145 through 152 984 41 227 288 616 103 710 453 Columns 153 through 160 899 93 445 794 811 712 933 147 Columns 161 through 168 449 91 326 656 816 614 644 983 Columns 169 through 176 596 495 928 319 883 378 108 11 Columns 177 through 184 880 374 913 994 402 602 969 13 Columns 185 through 192 940 418 717 987 55 154 63 680 Columns 193 through 200 311 290 990 939 892 957 934 362 Columns 201 through 208 472 309 582 823 88 117 429 343 Columns 209 through 216 662 415 254 40 380 496 284 397 Columns 217 through 224 553 149 212 168 674 283 810 799 Columns 225 through 232 217 909 499 414 121 730 264 199 Columns 233 through 240 211 356 861 21 148 301 115 197 Columns 241 through 248 359 303 23 44 189 584 26 182 Columns 249 through 256 548 255 805 47 930 316 629 59 Columns 257 through 264 160 963 932 86 280 270 663 33 Columns 265 through 272 437 531 341 31 841 436 351 807 Columns 273 through 280 144 381 232 554 143 855 572 30 Columns 281 through 288 809 776 116 519 464 520 181 258 Columns 289 through 296 236 473 427 174 180 266 102 165 Columns 297 through 304 545 982 779 320 122 352 702 25 Columns 305 through 312 679 504 658 659 839 843 367 417 Columns 313 through 320 95 812 262 511 396 713 135 451 Columns 321 through 328 938 988 67 800 391 476 949 744 Columns 329 through 336 331 432 24 361 401 248 130 581 Columns 337 through 344 670 462 480 739 267 195 785 772 Columns 345 through 352 634 114 516 788 547 307 701 510 Columns 353 through 360 605 698 568 458 637 60 852 179 Columns 361 through 368 184 960 783 571 123 291 844 1 Columns 369 through 376 881 422 6 177 446 253 724 479 Columns 377 through 384 789 348 500 719 878 452 339 905 Columns 385 through 392 274 133 849 305 295 350 566 461 Columns 393 through 400 20 185 819 420 753 225 456 651 Columns 401 through 408 302 699 555 685 293 69 607 860 Columns 409 through 416 312 70 628 525 506 920 247 835 Columns 417 through 424 532 124 335 241 999 194 15 19 Columns 425 through 432 489 207 752 234 608 112 743 853 Columns 433 through 440 410 250 126 782 951 426 125 778 Columns 441 through 448 627 824 85 672 917 104 501 558 Columns 449 through 456 598 759 671 873 329 868 958 617 Columns 457 through 464 904 737 549 599 173 508 386 825 Columns 465 through 472 43 826 498 770 756 746 387 706 Columns 473 through 480 87 543 394 105 910 798 840 622 Columns 481 through 488 51 405 895 375 718 593 536 433 Columns 489 through 496 535 34 170 470 265 761 669 162 Columns 497 through 504 57 944 395 169 560 529 986 688 Columns 505 through 512 639 292 831 857 198 7 1000 929 Columns 513 through 520 454 145 974 448 804 870 755 485 Columns 521 through 528 773 551 8 925 711 915 950 763 Columns 529 through 536 797 638 725 37 252 268 924 408 Columns 537 through 544 140 815 370 318 907 573 353 967 Columns 545 through 552 45 238 745 317 649 289 107 976 Columns 553 through 560 790 228 856 119 459 882 609 997 Columns 561 through 568 850 260 346 864 842 862 474 845 Columns 569 through 576 419 721 585 626 54 975 469 376 Columns 577 through 584 754 771 901 666 134 58 131 136 Columns 585 through 592 360 563 705 240 635 435 962 263 Columns 593 through 600 71 612 821 129 224 186 349 661 Columns 601 through 608 923 562 707 233 457 837 515 540 Columns 609 through 616 912 691 494 610 822 342 315 223 Columns 617 through 624 926 66 664 898 896 632 806 888 Columns 625 through 632 675 78 696 46 829 903 559 647 Columns 633 through 640 682 416 665 368 141 38 276 818 Columns 641 through 648 564 444 190 36 838 813 390 709 Columns 649 through 656 32 530 8...

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)

k = Columns 1 through 16 351 121 474 201 149 91 51 469 122 323 447 174 387 379 470 495 Columns 17 through 32 152 211 217 393 200 499 48 44 431 427 229 247 224 378 12 34 Columns 33 through 48 430 26 413 497 267 131 260 278 298 296 284 458 188 54 67 115 Columns 49 through 64 33 248 242 40 457 282 342 483 443 140 227 79 190 130 144 53 Columns 65 through 80 453 66 405 196 63 148 396 177 258 439 187 400 24 299 376 420 Columns 81 through 96 191 96 2 205 57 215 32 425 301 127 286 373 475 462 16 236 Columns 97 through 112 349 356 226 338 300 216 332 477 58 82 94 249 281 189 435 39 Columns 113 through 128 371 484 446 489 266 106 157 104 395 78 3 350 117 179 412 280 Columns 129 through 144 390 203 403 43 222 417 326 239 139 394 261 70 415 197 198 195 Columns 145 through 160 257 327 81 451 343 455 213 235 18 109 218 147 317 35 206 380 Columns 161 through 176 500 210 384 399 84 409 410 107 288 141 22 31 56 370 13 321 Columns 177 through 192 204 154 490 340 176 83 49 62 337 292 401 17 85 329 471 488 Columns 193 through 208 125 270 64 112 320 464 442 426 114 99 277 433 27 150 465 330 Columns 209 through 224 175 7 324 275 466 353 119 47 214 21 238 256 146 312 234 167 Columns 225 through 240 428 367 331 253 118 88 272 42 36 418 108 173 182 73 368 136 Columns 241 through 256 156 262 407 441 133 335 494 5 271 358 459 359 314 397 437 386 Columns 257 through 272 445 126 366 93 168 105 381 19 158 181 478 89 138 467 225 454 Columns 273 through 288 333 151 194 209 241 449 76 180 37 461 165 304 169 416 207 414 Columns 289 through 304 345 69 132 80 325 274 487 279 102 240 192 290 75 120 341 423 Columns 305 through 320 429 364 357 482 473 255 243 491 307 375 23 9 369 318 160 30 Columns 321 through 336 101 436 315 448 372 398 480 361 100 406 251 46 202 60 111 285 Columns 337 through 352 377 90 52 305 252 264 129 246 444 402 38 68 479 460 374 221 Columns 353 through 368 212 354 306 59 74 438 110 424 339 476 244 72 116 231 208 355 Columns 369 through 384 159 14 164 287 145 265 166 346 45 422 309 163 171 450 65 313 Columns 385 through 400 388 113 128 186 123 135 311 316 408 77 294 6 220 344 360 97 Columns 401 through 416 322 485 273 230 498 98 183 293 87 28 289 1 481 142 237 295 Columns 417 through 432 92 291 492 254 365 25 348 302 411 363 161 486 421 245 391 193 Columns 433 through 448 297 328 419 124 8 172 15 452 472 103 259 389 178 162 223 404 Columns 449 through 464 362 29 352 434 153 61 50 336 155 86 392 268 440 185 199 137 Columns 465 through 480 303 283 276 250 468 233 71 55 496 385 319 232 134 184 10 432 Columns 481 through 496 263 308 95 219 228 383 143 170 41 11 493 20 269 4 463 347 Columns 497 through 500 310 334 382 456 M = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 209 through 224 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 305 through 320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 321 through 336 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 337 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 368 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 369 through 384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 385 through 400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 401 through 416 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 417 through 432 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 433 through 448 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 449 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 480 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 497 through 500 0 0 0 0 M = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 209 through 224 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 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!