Borrar filtros
Borrar filtros

Binary to decimal using recursion

5 visualizaciones (últimos 30 días)
Rick
Rick el 12 de Ag. de 2014
Respondida: Salaheddin Hosseinzadeh el 12 de Ag. de 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

Respuestas (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 12 de Ag. de 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

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by