任何人都可以使用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);
任何人都可以使用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);
您可以尝试生成浮点数从[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进行观察。谢谢!)。
一些替代方案:
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]
感谢这么多有用的答案。我认为这个话题可能足够普遍,可能值得进行比较。
在我的设置(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')
只需用户randsrc功能。
它会产生1和-1的随机序列。
例如
出= randsrc(2,3)
出来=
-1 -1 -1
1 -1 1
我使用'randi'(1已经)喜欢samping方法,因为它允许采样通用人口 –
@LuisMendo - 谢谢!它确实基于'randsample',但'randsample'需要统计工具箱...我想我会提供一个不需要它的替代方案。 – rayryeng
@rayryeng对于sign(),randn几乎肯定不会生成0,所以我敢肯定,这种情况几乎无处不在,无处不在 – krisdestruction