Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Haseeb Hashim
Haseeb Hashim il 7 Mar 2022
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)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by