Azzera filtri
Azzera filtri

Binary to decimal using recursion

5 visualizzazioni (ultimi 30 giorni)
Rick
Rick il 12 Ago 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

Risposte (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh il 12 Ago 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

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by