필터 지우기
필터 지우기

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