I have vector of integer values : y = [ 1 2 3 4 5 ... 998 999 1000 ] , how can i generate binary random numbers with the same length for lower and upper of vector y ( i.e the binary value of 1000 is (0 0 0 1 0 1 1 1 1 1), and the binary value of 2 is ( 1 0 0 0 0 0 0 0 0 0 ) and so on .. This is the code y = [ 1 : 1000 ]; empty.value = []; out=repmat(empty.value,5,1);
for i = 1 : 5 % i want 5 binary numbers within the range 1 to 1000 and must be all the same length of bits a = randi(1000); b = de2bi(a); out(i).value = b ; end %% the results are different in number of bits {[1,1,1,1,1,1];[1,1,0,1,0,1,0,0,1];[1,1,1,1,0,1];[0,1,0,1,1,1,1,1,1];[0,1,0,1,1,1,1,1,0,1]}

 採用された回答

Stephen23
Stephen23 2018 年 3 月 21 日
編集済み: Stephen23 2018 年 3 月 21 日

0 投票

>> dec2bin(randi([1,1000],5,1),10)
ans =
1101100000
1000010111
1100010100
0000100001
1110110000
And to convert to numeric simply subtract '0':
dec2bin(...)-'0'

10 件のコメント

Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
Thank you so much...
Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
but this output is char i want it integer in order to convert it to decimal later
Andrei Bobrov
Andrei Bobrov 2018 年 3 月 21 日
dec2bin(randi([1,1000],5,1),10)-'0'
Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
thank you very very very much...
Stephen23
Stephen23 2018 年 3 月 21 日
@Ameer Kadhim: I hope that it helps. Remember to accept the answer that helped you most to resolve your question.
Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
Thank you Stephen Cobeldick very much.. i accept your answer
Birdman
Birdman 2018 年 3 月 21 日
I deleted mine then since this answer solves your problem.
Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
your answer do not solve my problem and i asked several times... thank you dir , i am sorry....
Ameer Kadhim
Ameer Kadhim 2018 年 3 月 21 日
@Stephen Cobeldick your answer not correct too .. see v = bi2de(dec2bin(randi([1,1000],1,1),10)-'0') v =
1013
i not want v greater than 1000 . ok (v must be less than 1000)
Wubie Engdew
Wubie Engdew 2019 年 8 月 13 日
>> dec2bin(randi([1,1000],5,1),10)
ans =
5×10 char array
'1100101111'
'1110001010'
'0001111111'
'1110010010'
'1001111001'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by