Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 356196

Submitted on 20 Nov 2013 by Ziko

Correct

41Size
Leading solution size is 11.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%% positive integers
x = 5;
assert(isequal(unique(rand_plusminus1(x)),[-1;1]))
y =
    0.4242    0.0292    0.2373    0.2316    0.3674
    0.5079    0.9289    0.4588    0.4889    0.9880
    0.0855    0.7303    0.9631    0.6241    0.0377
    0.2625    0.4886    0.5468    0.6791    0.8852
    0.8010    0.5785    0.5211    0.3955    0.9133
y =
    0.4242    0.0292    0.2373    0.2316    0.3674
    1.0000    1.0000    0.4588    0.4889    1.0000
    0.0855    1.0000    1.0000    1.0000    0.0377
    0.2625    0.4886    1.0000    1.0000    1.0000
    1.0000    1.0000    1.0000    0.3955    1.0000
y =
    -1    -1    -1    -1    -1
     1     1    -1    -1     1
    -1     1     1     1    -1
    -1    -1     1     1     1
     1     1     1    -1     1
2
Pass
 
%% one element
x = 1;
assert(isequal(abs(rand_plusminus1(x)),1))
y =
    0.7962
y =
     1
y =
     1
3
Pass
 
%% zero => empty matrix
x = 0;
assert(isequal(rand_plusminus1(x),[]))
y =
     []
4
Pass
 
%% positive integers
x = 999;
assert(isequal(unique(rand_plusminus1(x)),[-1;1]))
y =
  Columns 1 through 10
    0.0987    0.1387    0.0364    0.1653    0.5106    0.5256    0.6309    0.5526    0.0064    0.1118
    0.2619    0.4756    0.3262    0.9122    0.3852    0.2596    0.8920    0.6851    0.9199    0.5756
    0.3354    0.3625    0.9730    0.3192    0.3106    0.0512    0.6734    0.2997    0.0180    0.4617
    0.6797    0.7881    0.3650    0.3298    0.0036    0.7320    0.6853    0.6019    0.0294    0.5454
    0.1366    0.7803    0.3091    0.2042    0.8152    0.1643    0.6957    0.1985    0.7115    0.0380
    0.7212    0.6685    0.1209    0.7672    0.6384    0.2804    0.7998    0.6568    0.5384    0.5793
    0.1068    0.1335    0.9158    0.0700    0.4483    0.2594    0.6606    0.6984    0.5246    0.6290
    0.6538    0.0216    0.1355    0.9500    0.2441    0.5471    0.5199    0.4597    0.5022    0.4819
    0.4942    0.5598    0.3321    0.1582    0.8034    0.5413    0.3317    0.1577    0.0657    0.1605
    0.7791    0.3008    0.8975    0.2864    0.8240    0.7881    0.9342    0.4218    0.2300    0.7544
    0.7150    0.9394    0.4996    0.6871    0.8522    0.8696    0.2465    0.5961    0.1170    0.7935
    0.9037    0.9809    0.6153    0.1411    0.4673    0.7875    0.5112    0.3223    0.9898    0.2914
    0.8909    0.2866    0.5831    0.5121    0.9707    0.9694    0.7403    0.8307    0.0705    0.5076
    0.3342    0.8008    0.6983    0.7213    0.8412    0.1805    0.2542    0.1227    0.0600    0.2380
    0.6987    0.8961    0.0293    0.9288    0.0785    0.9306    0.8456    0.2512    0.6616    0.1927
    0.1978    0.5975    0.5279    0.7321    0.2376    0.0452    0.5382    0.9376    0.3441    0.8087
    0.0305    0.8840    0.0321    0.7498    0.8176    0.2406    0.9101    0.6552    0.1285    0.0667
    0.7441    0.9437    0.8271    0.4073    0.4058    0.0089    0.3507    0.7529    0.6371    0.1959
    0.5000    0.5492    0.3400    0.2395    0.4663    0.6716    0.9353    0.8109    0.7465    0.9748
    0.4799    0.7284    0.8467    0.5209    0.9515    0.9048    0.9277    0.0484    0.8053    0.5751
    0.9047    0.5768    0.2461    0.2191    0.9650    0.5724    0.5877    0.4147    0.2690    0.5832
    0.6099    0.0259    0.5815    0.8424    0.7653    0.1555    0.1018    0.7255    0.4340    0.9500
    0.6177    0.4465    0.9377    0.6629    0.5745    0.5024    0.3669    0.1390    0.4018    0.1000
    0.8594    0.6463    0.0478    0.8162    0.9159    0.5677    0.2758    0.6326    0.6081    0.8787
    0.8055    0.5212    0.0540    0.7939    0.4954    0.1883    0.2661    0.2468    0.7701    0.8915
    0.5767    0.3723    0.0206    0.4691    0.1660    0.3242    0.2265    0.8400    0.9411    0.2140
    0.1829    0.9371    0.6815    0.3095    0.3260    0.7160    0.0013    0.8367    0.1315    0.1670
    0.2399    0.8295    0.5986    0.6876    0.2964    0.5529    0.8930    0.4531    0.2557    0.6461
    0.8865    0.8491    0.1140    0.9869    0.5583    0.1423    0.4535    0.3948    0.3783    0.1803
    0.0287    0.3725    0.7962    0.7699    0.0675    0.3804    0.5784    0.9616    0.9937    0.1165
    0.4899    0.5932    0.6179    0.8296    0.0690    0.3966    0.3156    0.0082    0.3410    0.3653
    0.1679    0.8726    0.0702    0.7061    0.1668    0.5767    0.9940    0.3984    0.8996    0.4024
    0.9787    0.9335    0.0693    0.5953    0.9474    0.0194    0.9839    0.4878    0.2375    0.0898
    0.7127    0.6685    0.1360    0.7529    0.8111    0.5776    0.9645    0.6181    0.2200    0.6838
    0.5005    0.2068    0.7889    0.4967    0.7105    0.9322    0.6663    0.0700    0.9911    0.5469
    0.4711    0.6539    0.0924    0.8651    0.9702    0.1069    0.7262    0.1427    0.9511    0.9860
    0.0596    0.0721    0.2379    0.0680    0.9984    0.7321    0.3340    0.6542    0.6382    0.2127
    0.6820    0.4067    0.2436    0.9685    0.9875    0.9705    0.5228    0.2183    0.5041    0.9190
    0.0424    0.6669    0.1048    0.0988    0.1501    0.6089    0.2734    0.1191    0.3581    0.6652
    0.0714    0.9337    0.8584    0.5470    0.9585    0.7197    0.7184    0.0431    0.7685    0.7435
    0.5216    0.8110    0.6982    0.4030    0.5305    0.3028    0.7780    0.1656    0.7844    0.3785
    0.0967    0.4845    0.7337    0.1070    0.0741    0.4590    0.0811    0.5111    0.0289    0.4748
    0.8181    0.7567    0.6505    0.7242    0.3118    0.0480    0.2216    0.8673    0.0524    0.1063
    0.8175    0.4170    0.5163    0.6137    0.8952    0.3854    0.2040    0.7663    0.3234    0.8838
    0.7224    0.9718    0.3264    0.7830    0.8348    0.3617    0.6241    0.1350    0.7997    0.4451
    0.1499    0.9880    0.6618    0.5666    0.0023    0.2876    0.7252    0.0649    0.6302    0.6682
    0.6596    0.8641    0.1176    0.8113    0.6402    0.8167    0.8344    0.6724    0.9828    0.7282
    0.5186    0.3889    0.1478    0.5768    0.8032    0.4505    0.0189    0.4822    0.1582    0.2856
    0.9730    0.4547    0.0198    0.9440    0.2451    0.8066    0.2021    0.4954    0.6167    0.5937
    0.6490    0.2467    0.9643    0.8715    0.0641    0.7902    0.4691    0.3135    0.3087    0.3705
    0.8003    0.7844    0.9704    0.5076    0.2631    0.2830    0.3784    0.3235    0.0851    0.7956
    0.4538    0.8828    0.1239    0.7888    0.1027    0.0683    0.3404    0.9091    0.8768    0.0735
    0.4324    0.9137    0.4674    0.4730    0.4837    0.0549    0.0639    0.4503    0.5907    0.9927
    0.8253    0.5583    0.6567    0.8288    0.4189    0.6375    0.7613    0.7486    0.3031    0.7760
    0.0835    0.5989    0.2902    0.3225    0.3813    0.4243    0.4027    0.1853    0.1681    0.0074
    0.1332    0.1489    0.7545    0.9761    0.8868    0.9055    0.6743    0.0849    0.3399    0.8889
    0.1734    0.8997    0.5581    0.2782    0.4206    0.4173    0.5511    0.2070    0.0674    0.8641
    0.3909    0.4504    0.4278    0.0728    0.2838    0.1541    0.0515    0.5589    0.6535    0.6976
    0.8314    0.2057    0.2672    0.7512    0.0482    0.5400    0.3075    0.6617    0.2443    0.8850
    0.8034    0.8997    0.7537    0.8312    0.2192    0.9371    0.9654    0.9392    0.7579    0.4656
    0.0605    0.7626    0.8984    0.9223    0.2392    0.6610    0.9315    0.0402    0.2990    0.3716
    0.3993    0.8825    0.7284    0.3270    0.0293    0.3947    0.3780    0.1637    0.4188    0.0830
    0.5269    0.2850    0.4068    0.8041    0.7023    0.2590    0.6177    0.4381    0.0558    0.9972
    0.4168    0.6732    0.9383    0.5383    0.0076    0.8479    0.5625    0.3257    0.0309    0.8698
    0.6569    0.6643    0.2554    0.4633    0.6109    0.9451    0.8306    0.0899    0.1866    0.0001
    0.6280    0.1228    0.5332    0.8208    0.4081    0.3770    0.9577    0.8103    0.2709    0.6800
    0.2920    0.4073    0.9548    0.9519    0.2489    0.0673    0.0755    0.3996    0.0662    0.6549
    0.4317    0.2753    0.2677    0.0763    0.6525    0.1816    0.8880    0.6553    0.3335    0.4118
    0.0155    0.7167    0.2501    0.7087    0.3203    0.5757    0.5624    0.5668    0.0499    0.4764
    0.9841    0.2834    0.9277    0.2349    0.1037    0.1859    0.1944    0.0031    0.1013    0.3883
    0.1672    0.8962    0.0686    0.3989    0.5356    0.2914    0.2215    0.6514    0.0719    0.4521
    0.1062    0.8266    0.2994    0.2681    0.1649    0.4617    0.7058    0.3171    0.9045    0.0268
    0.3724    0.3900    0.5916    0.8325    0.8834    0.3470    0.5968    0.2659    0.3118    0.4253
    0.1981    0.4979    0.2033    0.9954    0.6665    0.3182    0.5863    0.4604    0.5918    0.1058
    0.4897    0.6948    0.6359    0.6498    0.8477    0.4599    0.9685    0.6232    0.3776    0.0431
    0.3395    0.8344    0.7984    0.7040    0.7627    0.2359    0.5817    0.8019    0.9516    0.5561
    0.9516    0.6096    0.5017    0.9323    0.8070    0.0278    0.0998    0.5612    0.7424    0.6113
    0.9203    0.5747    0.6508    0.6877    0.6330    0.6585    0.1666    0.0248    0.8871    0.7388
    0.0527    0.3260    0.7960    0.5684    0.7104    0.1588    0.1021    0.7935    0.5989    0.2569
    0.7379    0.4564    0.2334    0.3808    0.6887    0.8027    0.1462    0.2731    0.6896    0.7263
    0.2691    0.7138    0.6008    0.6346    0.3209    0.4086    0.6716    0.8229    0.3911    0.7133
    0.4228    0.8844    0.1125    0.3632    0.5316    0.3274    0.6399    0.6886    0.2233    0.5038
    0.5479    0.7209    0.5158    0.4076    0.8732    0.7460    0.3724    0.6039    0.1313    0.8928
    0.9427    0.0186    0.8378    0.3687    0.0545    0.7464    0.1629    0.3870    0.1725    0.7794
    0.4177    0.6748    0.9208    0.4684    0.5004    0.1740    0.3895    0.0655    0.9386    0.4949
    0.9831    0.4385    0.4982    0.5034    0.4328    0.1175    0.8000    0.9986    0.0345    0.7615
    0.3015    0.4378    0.2776    0.9105    0.9043    0.1740    0.3998    0.6610    0.8796    0.6670
    0.7011    0.1170    0.6525    0.2064    0.6302    0.6274    0.7551    0.2292    0.5223    0.9781
    0.6663    0.8147    0.9173    0.3386    0.9830    0.8419    0.2952    0.0923    0.8082    0.1171
    0.5391    0.3249    0.5098    0.5741    0.5852    0.5101    0.6400    0.2410    0.4476    0.8693
    0.6981    0.2462    0.9742    0.4869    0.8406    0.1658    0.8851    0.9026    0.3465    0.5679
    0.6665    0.3427    0.1973    0.2622    0.4688    0.7143    0.2096    0.2173    0.7572    0.1202
    0.1781    0.3757    0.1112    0.5796    0.5452    0.9070    0.4804    0.4795    0.3602    0.9703
    0.1280    0.5466    0.2974    0.8783    0.1791    0.2185    0.1130    0.9092    0.5566    0.8400
    0.9991    0.5619    0.3964    0.0610    0.6345    0.8710    0.1324    0.1938    0.8887    0.0623
    0.1711    0.3958    0.4208    0.4409    0.9630    0.2118    0.0640    0.4480    0.8402    0.2781
    0.0326    0.3981    0.3115    0.0843    0.5340    0.8367    0.0793    0.6178    0.2440    0.4096
    0.5612    0.5154    0.6938    0.5632    0.4796    0.8593    0.6270    0.5942    0.4356    0.6268
    0.8819    0.6575    0.0919    0.5393    0.7937    0.5234    0.4113    ...