필터 지우기
필터 지우기

how to generate a vector of zeros with 2 random successives cell containing ones

조회 수: 1 (최근 30일)
loukil sana
loukil sana 2019년 4월 24일
답변: Rik 2019년 4월 24일
how to generate a vector of zeros with 2 random successives cell containing ones

답변 (2개)

Star Strider
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

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

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by