how to generate a vector of zeros with 2 random successives cell containing ones
1 次查看(过去 30 天)
显示 更早的评论
how to generate a vector of zeros with 2 random successives cell containing ones
1 个评论
Adam Danz
2019-4-24
What does "2 random successives cell" mean?
Vector of zeros
zeros(1,15)
Vector of ones
ones(0,15)
回答(2 个)
Star Strider
2019-4-24
I have no idea what you want.
Try this:
z = zeros(1, 20); % Vector Of 20 Zeros
idx = randi(numel(z)-1); % Choose An Index
z(idx:idx+1) = 1; % Define Consecutive Indices = 1
0 个评论
Rik
2019-4-24
If you don't mean cell, but element:
n=10;%vector length
v=zeros(1,n);
one_ind=randi(n-1,1);
v(one_ind+[0 1])=1;
If you do want a cell array:
n=10;%vector length
v=zeros(1,n);
one_ind=randi(n-1,1);
v(one_ind+[0 1])=1;
v=num2cell(v);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!