From: James Tursa on 29 Apr 2010 23:45 "AS" <as(a)as.as> wrote in message <hrd6np$bol$1(a)fred.mathworks.com>... > Hi All, > > I'm wondering if there is a way to return the digits of a number (up to a certain length) as a list. > > For example: > 3.14159 -> [3, 1, 4, 1, 5, 9] > 10214.245 -> [1, 0, 2, 1, 4, 2, 4, 5] > > Mathematica has a function called RealDigits which does exactly this, so I'm wondering if there's some nice way to do it in Matlab. > > Reference: http://reference.wolfram.com/mathematica/ref/RealDigits.html > > Thanks! Here is my contribution to this thread. It uses my FEX submission num2strexact. This gets you every decimal digit using an exact binary to decimal conversion ... not sure if that is what you want or not. James Tursa ---------------------------------------------------- function c = chardigits(x) s = num2strexact(x); if( s(1) == '-' ) s = s(2:end); end e = find(s=='e'); if( isempty(e) ) e = length(s) + 1; y = 0; else y = str2double(s(e+1:end)); end p = find(s=='.'); if( isempty(p) ) p = 0; end y = max(y - (e - p),0); c = [s(1:p-1) s(p+1:e-1) repmat('0',1,y)]; end -------------------------------------------------------------- num2strexact can be found here: http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
|
Pages: 1 Prev: Schwarz Christoffel Help Next: Need help for Finding clustering efficiency |