Filter löschen
Filter löschen

Binary to decimal using recursion

5 Ansichten (letzte 30 Tage)
Rick
Rick am 12 Aug. 2014
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

Antworten (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh am 12 Aug. 2014
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

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by