【问题标题】:Generating a Pseudo-random sequence of plus/minus 1 integers生成加/减 1 整数的伪随机序列
【发布时间】:2015-06-23 13:43:47
【问题描述】:

谁能帮我用Matlab创建一个长度为1000的+-1整数的简单伪随机序列?

即诸如

之类的序列
-1 -1 1 1 -1 -1 1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 

我尝试使用下面的代码,但这是范围 -1 到 1,其中包括 0 个值。我只想要 -1 和 1。谢谢

x = randi([-1 1],1000,1);

【问题讨论】:

    标签: matlab random sequence prng


    【解决方案1】:

    您可以尝试从[0,1] 生成随机浮点数序列,并将小于 0.5 的任何值设置为 -1,将任何更大的值设置为 1:

    x = rand(1000,1);
    ind = x >= 0.5;
    x(ind) = 1;
    x(~ind) = -1;
    

    我的另一个建议是将sign 函数与randn 结合使用,这样我们就可以生成正数和负数。 sign 根据输入的符号生成 -1、0、1 的值。如果输入为负,则输出为 -1,当为正时为 +1,当为 0 时为 0。您可以额外检查输出为 0 的任何值,将它们设置为 -1 或 1:

    x = sign(randn(1000,1));
    x(x == 0) = 1;
    

    还有一个(受 Luis Mendo 启发)是有一个 [-1,1] 的向量并使用 randi 生成一个 1 或 2 的序列,然后使用它并采样到这个向量中:

    vec = [-1 1];
    x = vec(randi(numel(vec), 1000, 1));
    

    此代码可以扩展,vec 可以是您想要的任何东西,我们可以从vec 中的任何元素中采样以产生随机的值序列(Luis Mendo 的观察。谢谢!)。

    【讨论】:

    • 我喜欢使用randi(已经+1)的抽样方法,因为它允许对一般人群进行抽样
    • @LuisMendo - 谢谢!它确实基于randsample,但randsample 需要统计工具箱...我想我会提供一个不需要它的替代方案。
    • @krisdestruction - 我在考虑这个......但你永远不会知道。我不喜欢小机会 :P 有这种可能性!
    • @krisdestruction - .... 仍然有这种可能性!但是,感谢您的计算验证:)
    • 谢谢大家!使用 vec 的第三个建议很漂亮! :D
    【解决方案2】:

    一些替代方案:

    x = 2*randi(2, 1000, 1)-3; %// generate 1 and 2 values, and transform to -1 and 1
    x = 2*(rand(1, 1000, 1)<=.5)-1; %// similar to Rayryeng's answer but in one step
    x = randsample([-1 1], 1000, true); %// sample with replacement from the set [-1 1]
    

    【讨论】:

    • @rayryeng 哎呀。我忘记了“&lt;=.5”和rand。现已更正。谢谢!
    【解决方案3】:

    感谢您提供这么多有用的答案。我认为这个主题可能足够笼统,值得比较。

    在我的设置(Windows8.4 x64 i74820k cpu 和 R2014a)中,最快的版本始终是:

    x=2*round(rand(L,1))-1;
    

    比最慢的解决方案快半个数量级。希望这会有所帮助。

    比较: figure comparing execution times for pseudo-random sign generation

    代码:

    L=[];
    for expon=0:6
        for mant=1:9
        L=cat(1,L,mant*power(10,expon));
        end
    end
    clear expon mant
    
    t1=zeros(length(L),1);
    x=2*round(rand(L(1),1))-1;
    for li=1:length(L)
        tic,
        x=2*round(rand(L(li),1))-1;
        t1(li)=toc;
    end
    
    t2=zeros(length(L),1);
    x=(rand(L(1),1)>0.5)*2-1;
    for li=1:length(L)
        tic,
        x=(rand(L(li),1)>0.5)*2-1;
        t2(li)=toc;
    end
    
    t3=zeros(length(L),1);
    x=(randi([0,1],L(1),1)>0.5)*2-1;
    for li=1:length(L)
        tic,
        x=(randi([0,1],L(li),1)>0.5)*2-1;
        t3(li)=toc;
    end
    
    t4=zeros(length(L),1);
    x=rand(L(1),1);ind=x>=0.5;x(ind)=1;x(~ind)=-1;
    for li=1:length(L)
        tic,
        x=rand(L(li),1);
        ind=x>=0.5;
        x(ind)=1;
        x(~ind)=-1;
        t4(li)=toc;
    end
    
    t5=zeros(length(L),1);
    x=sign(randn(L(1),1));
    for li=1:length(L)
        tic,
        x=sign(randn(L(li),1));
        x(x==0)=1;
        t5(li)=toc;
    end
    
    t6=zeros(length(L),1);
    vec = [-1 1];
    x=vec(randi(numel(vec),L(1),1));
    for li=1:length(L)
        tic,
        x=vec(randi(numel(vec),L(li),1));
        t6(li)=toc;
    end
    
    t7=zeros(length(L),1);
    x=2*randi(2,L(1),1)-3;
    for li=1:length(L)
        tic,
        x=2*randi(2,L(li),1)-3;
        t7(li)=toc;
    end
    
    t8=zeros(length(L),1);
    x=randsample([-1 1],L(1),true);
    for li=1:length(L)
        tic,
        x=randsample([-1 1],L(li),true);
        t8(li)=toc;
    end
    
    clear x vec ind li
    
    figure,
    loglog(L,[t1 t2 t3 t4 t5 t6 t7 t8],'.-','linewidth',2)
    grid on 
    grid minor
    title('Generating pseudo-random sequence +1/-1')
    ylabel('Exec. Time [s]')
    xlabel('Output Vector Length')
    T{1}='x=2*round(rand(L(1),1))-1';
    T{2}='x=(rand(L(1),1)>0.5)*2-1';
    T{3}='x=(randi([0,1],L(1),1)>0.5)*2-1';
    T{4}='x=rand(L(1),1);ind=x>=0.5;x(ind)=1;x(~ind)=-1';
    T{5}='x=sign(randn(L(1),1))';
    T{6}='vec=[-1 1];x=vec(randi(numel(vec),L(1),1))';
    T{7}='x=2*randi(2,L(1),1)-3';
    T{8}='x=randsample([-1 1],L(1),true)';
    legend(T,'location','northwest')
    

    【讨论】:

      【解决方案4】:

      简单的用户randsrc函数。

      它将生成1和-1的随机序列。

      例如

      out = randsrc(2,3)

      输出 =

      -1    -1    -1
       1    -1     1
      

      【讨论】:

        【解决方案5】:
        x = rand(N,1);
        y = sign(x-0.5);
        

        【讨论】:

          猜你喜欢
          • 2011-11-03
          • 1970-01-01
          • 2015-08-07
          • 1970-01-01
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 2016-10-23
          • 2014-05-18
          相关资源
          最近更新 更多