creating binary vector with determed number of ones

Hello,
i have a vector V= 50; i want to full this vector randomly with 0 and 1. the '1' appears 10 times and the rest of the vector with zeros. how can i do that please

 Akzeptierte Antwort

John D'Errico
John D'Errico am 24 Apr. 2019
Bearbeitet: John D'Errico am 24 Apr. 2019
nones = 10;
nv = 50;
V = zeros(1,nv);
V(randperm(nv,nones)) = 1;

3 Kommentare

Thank you.
Sorry i have an other question.
Now if i have a matrix M= (N,L) =(20,30) and i want to allocate some cells of this matrix
exemple: V1 = 1 1 1: means V1 will take a place in the matrix M starting at L=1
1 0 0
v2= 1 1 1 1 : same for this matrix , V2 take place in the matrix M starting at L=1
1 0 0 0 means the first column of V2 will be in the first
1 0 0 0 column of M
I'm sorry. I cannot/will not keep on chasing totally new questions in the comments. That turns one question into an extended personal consulting session. If you have another question, then ask it. AS ANOTHER QUESTION.
ok no problem thank you. i will post it as a new question

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by