How can I create a random vector with at most 1 in it?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ezz El-din Abdullah
am 27 Jun. 2018
Kommentiert: Ezz El-din Abdullah
am 27 Jun. 2018
I tried to make a random vector of 0's and 1's like the code below:
v = round(rand(1,n)) % where n is the dimension of the vector
But I'd like to give it a condition to avoid the existence of 1 more than one time. Also, I want the index to be selected randomly.
For example, this:
[1 1 0 0] % where n = 4
is not accepted, but this:
[1 0 0 0]
%or
[0 1 0 0]
either of them will be accepted.
So any help please? Thanks!
0 Kommentare
Akzeptierte Antwort
Guillaume
am 27 Jun. 2018
Bearbeitet: Guillaume
am 27 Jun. 2018
Another option:
v = randperm(n) == n
or
v = randperm(n) == 1
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!