Decomposition in any base Matlab
이전 댓글 표시
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!