create a binary sequence that consisting of m zeros and n ones in any order.
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (3)
Roger Stafford
el 23 de Nov. de 2014
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 comentarios
Bear
el 25 de Nov. de 2014
Image Analyst
el 25 de Nov. de 2014
You may have an old version where randperm() did not allow a second input argument. Please upgrade.
Azzi Abdelmalek
el 25 de Nov. de 2014
There is an error in
p = randperm(1:m+n,n);
Roger Stafford
el 26 de Nov. de 2014
Yes, it should have been
p = randperm(m+n,n);
Azzi Abdelmalek
el 23 de Nov. de 2014
Editada: Azzi Abdelmalek
el 23 de Nov. de 2014
x1=[zeros(1,5) ones(1,4)]
%or
N=5;
n=4;
X1=1:n+N>N
4 comentarios
Bear
el 25 de Nov. de 2014
Azzi Abdelmalek
el 25 de Nov. de 2014
N=5;
M=4;
X1=zeros(1,N+M);
X1(randperm(N+M,M))=1
Bear
el 25 de Nov. de 2014
Bear
el 27 de Nov. de 2014
Jan
el 23 de Nov. de 2014
And another apporach:
x1 = zeros(1, n+m);
x1(n+1:n+m) = 1;
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!