Decomposition in any base Matlab
Show older comments
Hi, I would like to know if there was a Matlab builtin function that decompose any integer in a chosen base, it would be something like:
dec2base(52,4)
ans =
0 1 3
I've write my own function, but it's pretty slow. Maybe you will detect an error in my code (nVariable is the length of the array I want my function to return):
function [digits] = LeftDecompose(number, base, nVariable)
% Decompose a number in any base.
quotient = floor(number/base^(nVariable-1));
remainder = number - quotient * base^(nVariable-1);
if remainder == 0
digits = [zeros(1, nVariable-1) quotient];
else
digits = [LeftDecompose(remainder, base, nVariable - 1) quotient];
end
end
Thank you in advance.
Accepted Answer
More Answers (0)
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!