How to generate a a restricted binary number
1 回表示 (過去 30 日間)
古いコメントを表示
採用された回答
Jan
2018 年 12 月 5 日
編集済み: Jan
2018 年 12 月 5 日
What exactly is a "number include 24 bits"?
This sets 4 elements to 1 and 20 to 0:
v = zeros(1, 24);
v(randperm(24, 4)) = 1;
What is the wanted output?
result = bin2dec('0' + v)
% or
result = v * power(2, 0:23).'
Instead of creating the sum using the dot product, you can use the indices replied by randperm directly:
result = sum(2 .^ (randperm(24, 4) - 1));
I hope this was not a homework question. Otherwise it will be hard for you to submit your own solution now. But you participate in this forum for 6 years now, such that I assume that you are not in the learning phase anymore.
0 件のコメント
その他の回答 (1 件)
Mohsen
2018 年 12 月 5 日
編集済み: Mohsen
2018 年 12 月 5 日
2 件のコメント
Jan
2018 年 12 月 6 日
編集済み: Jan
2018 年 12 月 6 日
I do not understand what "number dimension, max and min" means. Maximum and minimum of what? My answer includes a method to create random vectors already:
v = zeros(1, 24);
v(randperm(24, 4)) = 1;
Do you now want to create all these vectors? Then:
M = nchoosek(1:24, 4);
for k = 1:size(M, 1)
v = zeros(1, 24);
v(M(k, :)) = 1;
...
end
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!