2015-04-16 55 views
2

任何人都可以使用Matlab創建一個長度爲1000的+ -1整數的簡單僞隨機序列嗎?生成加/減1整數的僞隨機序列

I.e.一個序列如

-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); 

回答

5

您可以嘗試生成浮點數從[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; 

一個以上(由路易斯Mendo啓發)。將具有[-1,1]一個矢量,並使用randi生成的1或2的序列,然後使用這個和樣品入該載體:

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

這代碼可以擴展,其中vec可以是任何你想要的,w e可以從vec中的任何元素中採樣以產生一個隨機的值序列(由Luis Mendo進行觀察。謝謝!)。

+1

我使用'randi'(1已經)喜歡samping方法,因爲它允許採樣通用人口 –

+0

@LuisMendo - 謝謝!它確實基於'randsample',但'randsample'需要統計工具箱...我想我會提供一個不需要它的替代方案。 – rayryeng

+0

@rayryeng對於sign(),randn幾乎肯定不會生成0,所以我敢肯定,這種情況幾乎無處不在,無處不在 – krisdestruction

3

一些替代方案:

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] 
+0

這就是東西! – rayryeng

+0

@rayryeng哎呀。我用'rand'忘記了「<=。5」。現在更正。謝謝! –

+0

沒問題!它現在有效。 – rayryeng

0

感謝這麼多有用的答案。我認爲這個話題可能足夠普遍,可能值得進行比較。

在我的設置(Windows8.4 64 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') 
0

只需用戶randsrc功能。

它會產生1和-1的隨機序列。

例如

出= randsrc(2,3)

出來=

-1 -1 -1 
1 -1  1