Effacer les filtres
Effacer les filtres

Binary to decimal using recursion

5 vues (au cours des 30 derniers jours)
Rick
Rick le 12 Août 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

Réponses (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh le 12 Août 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

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by