Skip to content

Instantly share code, notes, and snippets.

@robince
Created August 11, 2013 09:07

Revisions

  1. robince created this gist Aug 11, 2013.
    19 changes: 19 additions & 0 deletions numdec2base.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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'));