这是我的代码的一部分
e=0;
c=0;
n=10000;
for t=zeros(1:n)
//state1
x=rand();
if(x<=0.95) then disp(t);
c=c+1;
elseif(x>0.95)
//state2
x=rand();
if(x<=0.99) then disp(t)
c=c+1;
//state3
elseif(x>0.99) then disp(t=1)
e=e+1;
arr(e)=t; //store error bits only
end
end
end
disp(c);
disp(e);
for z=1:e //loop the earlier arr(s)
disp(arr(z)) //display all arr of s
end
clear();
我想要做的是生成10000个零。 在这些10000个零中,很少会出现错误,例如我可能会得到9990个零和10个零。
目前,我已经制作了一个只存储那些数组的数组。现在我失去了如何将零和一个存储到同一个数组中。
假设当前正在运行..我最终会得到10个(那些包含错误位的零)。然后在代码的这一部分,所有变为1的零将存储到arr(e)中。因此输出将是
0
0
0
0
0
0
0
0
0
0
但我想要的是这样的。
arr[1] = 0
.
.
.
arr[250] = 1
.
.
.
arr[749] = 1
.
.
.
arr[1234] = 1
.
.
.
arr[5463] = 1
.
.
.
arr[6678] = 1
.
.
.
arr[8890] = 1
.
.
.
arr[9987] = 1
.
.
.
arr[10000] = 0
其中显示错误位出现在250,749,1234,5463,6678,8890,9987
谢谢
答案 0 :(得分:2)
您所要做的就是:
e = [250 759 1234 5463 6678 8890 9987];
arr = zeros(10000,1);
arr(e) = 1;
e
定义了您希望arr
中的值更改为1的位置。您只需使用e
索引到arr
并将相应的位置设置为1那就是......真的没事!