Created
August 11, 2013 09:07
-
-
Save robince/6204098 to your computer and use it in GitHub Desktop.
numdec2base.m: optimized numerical dec2base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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