Skip to content

Instantly share code, notes, and snippets.

@robince
Created August 11, 2013 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robince/6204098 to your computer and use it in GitHub Desktop.
Save robince/6204098 to your computer and use it in GitHub Desktop.
numdec2base.m: optimized numerical dec2base
function x = numdec2base(d,b,m)
%NUMDEC2BASE Convert decimal univariate integer to base B multivariate vector.
% NUMDEC2BASE(D,B,M) returns the representation of D as a length-M base-B
% word.
%
% Examples
% numdec2base(23,3) returns [2;1;2]
% numdec2base(23,3,5) returns [0;0;2;1;2]
%
% See also NUMBASE2DEC
[m_in, Nt] = size(d);
if m_in ~= 1
error('NUMDEC2BASE: input must be row vector')
end
z = b.^( (m-1):-1:0 );
x = floor(bsxfun(@rdivide, bsxfun(@rem,d,b*z'), z'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment