필터 지우기
필터 지우기

Binary to decimal using recursion

조회 수: 5 (최근 30일)
Rick
Rick 2014년 8월 12일
답변: Salaheddin Hosseinzadeh 2014년 8월 12일
Hello, I'm trying to figure out how to write a function that will convert binary to decimal using recursion. BA is a 1 x n array of binary digits
function y = Bin2dec(BA)
n = length(BA);
if n == 1
y = BA;
else
y = Bin2dec(BA(n-1));
end
but I can't figure out the recursive part

답변 (1개)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014년 8월 12일
Hi Rick,
You're aware of the MATLAB built in bin2dec function ain't u?
I've no idea if it's possible to call "Bin2dec" function from "Bin2dec" function?!!!
change the function name so that it won't interfere with matlab function
function y = _bin2dec(BA)
y = 0; BA = num2str(BA);
for n = 1:length(BA)
y = y + str2num(BA(n))^n-1;
end
end
hopefully that will do! ;)
Good Luck

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by