how to convert decimal to binary ?
2 次查看(过去 30 天)
显示 更早的评论
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]}
0 个评论
采纳的回答
Stephen23
2018-3-21
编辑:Stephen23
2018-3-21
>> dec2bin(randi([1,1000],5,1),10)
ans =
1101100000
1000010111
1100010100
0000100001
1110110000
And to convert to numeric simply subtract '0':
dec2bin(...)-'0'
10 个评论
Wubie Engdew
2019-8-13
>> dec2bin(randi([1,1000],5,1),10)
ans =
5×10 char array
'1100101111'
'1110001010'
'0001111111'
'1110010010'
'1001111001'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!