How can I create a random vector with at most 1 in it?
1 回表示 (過去 30 日間)
古いコメントを表示
Ezz El-din Abdullah
2018 年 6 月 27 日
コメント済み: Ezz El-din Abdullah
2018 年 6 月 27 日
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 件のコメント
採用された回答
Guillaume
2018 年 6 月 27 日
編集済み: Guillaume
2018 年 6 月 27 日
Another option:
v = randperm(n) == n
or
v = randperm(n) == 1
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!