create a binary sequence that consisting of m zeros and n ones in any order.

조회 수: 6 (최근 30일)
I need help to program a matlab code to generate a binary sequence that contains m zeros and n ones. I created a N lengths of zero, x1=zeros(1,N), but how to add n ones into the sequence of zeros.

채택된 답변

Star Strider
Star Strider 2014년 11월 23일
The easiest way:
x1 = [zeros(1,N) ones(1,M)];
  댓글 수: 1
Bear
Bear 2014년 11월 25일
Thx alot and it works well, but I forgot to say the zeros and ones have to be placed in random order. Do you have any ideas how to do this.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Roger Stafford
Roger Stafford 2014년 11월 23일
Or perhaps you want them in random order:
x = zeros(1,m+n);
p = randperm(1:m+n,n);
x(p) = ones(1,n);
  댓글 수: 4

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
편집: Azzi Abdelmalek 2014년 11월 23일
x1=[zeros(1,5) ones(1,4)]
%or
N=5;
n=4;
X1=1:n+N>N
  댓글 수: 4
Bear
Bear 2014년 11월 25일
Thank you. That was what I asked for.
Bear
Bear 2014년 11월 27일
There is a question on my further programming this code, For example, m=n=2. then the number of combinations of the binary sequence is 6,nchoosek(4,2). My code was fine to output the 6 combinations. However, the combinations are not unique. one or two combinations show twice. Here is the detail of question, you may want to click and take a look. generate the binary sequence Again, thank you for the great helps.

댓글을 달려면 로그인하십시오.


Jan
Jan 2014년 11월 23일
And another apporach:
x1 = zeros(1, n+m);
x1(n+1:n+m) = 1;

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by