I am converting decimal to binary. Then backward filling the binary in x2 array

2 次查看(过去 30 天)
cinit=9;
rem=dec2bin(cinit);
rem=str2num(rem);
length=numel(num2str(rem))
for i=1:1:length
x2(i)=mod(rem,10);
rem=rem/10;
end
disp(x2(1:8));
Output for the code:
1.0000 0.1000 0.0100 1.0010 0 0 0 0
Expected Output :
1.0000 0.0000 0.0000 1.0000 0 0 0 0

回答(1 个)

Haseeb Hashim
Haseeb Hashim 2022-3-7
Use this code
clc
clear
close all
num = 200;
i = 1;
flag = true;
while flag == true
bin(i) = rem(num,2);
if bin(i) == 0
num = num/2;
if num == 1
bin(i+1) = 1;
break
end
elseif bin(i) ~= 0
num = floor(num/2);
if num == 1
bin(i+1) = 1;
break
end
end
i = i + 1;
end
flip(bin)

类别

Help CenterFile 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