From: AS on 29 Apr 2010 20:03 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!
From: dpb on 29 Apr 2010 20:17 AS wrote: > 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] .... One straightahead way... digitspi = sscanf(strrep(num2str(pi,8),'.',''),'%1d')' --
From: AS on 29 Apr 2010 20:46 dpb <none(a)non.net> wrote > One straightahead way... > digitspi = sscanf(strrep(num2str(pi,8),'.',''),'%1d')' That's intriguing... It would be nice, however, to not have to specify things like the 8 for the number of digits and the %1d telling it that the decimal occurs after the first digit. I.e., I'd like the function to work for arbitrary numbers. Finding the decimal point is not hard, as one can just use num2str and then find '.'. But the problem still remains as to discovering how many digits there are in a number. (There's an obvious issue here with non-terminating decimals, but let's ignore that for now.) So, the question now reduces to finding a function to give me the number of digits in a number. For example: 12353 --> 5 32.7182 --> 6 ..9945 --> 4 Any thoughts on that?
From: TideMan on 29 Apr 2010 21:14 On Apr 30, 12:46 pm, "AS" <a...(a)as.as> wrote: > dpb <n...(a)non.net> wrote > > > One straightahead way... > > digitspi = sscanf(strrep(num2str(pi,8),'.',''),'%1d')' > > That's intriguing... > > It would be nice, however, to not have to specify things like the 8 for the number of digits and the %1d telling it that the decimal occurs after the first digit. I.e., I'd like the function to work for arbitrary numbers.. > > Finding the decimal point is not hard, as one can just use num2str and then find '.'. But the problem still remains as to discovering how many digits there are in a number. (There's an obvious issue here with non-terminating decimals, but let's ignore that for now.) > > So, the question now reduces to finding a function to give me the number of digits in a number. For example: > 12353 --> 5 > 32.7182 --> 6 > .9945 --> 4 > > Any thoughts on that? str=num2str(number); digits=double(str) - 48; % . is -2 % Length is: sum(digits>=0)
From: Walter Roberson on 29 Apr 2010 21:43 AS wrote: > dpb <none(a)non.net> wrote >> One straightahead way... >> digitspi = sscanf(strrep(num2str(pi,8),'.',''),'%1d')' > > That's intriguing... > > It would be nice, however, to not have to specify things like the 8 for > the number of digits That tells me that you do not understand that Matlab stores numbers in binary floating point. >> sprintf('%.99g',pi) ans = 3.141592653589793115997963468544185161590576171875 That is 50 digits including the '.' However, only 53 bits of mantissa are stored in a 64 bit floating point number, which is roughly 16 decimal digits of accuracy: >> num2hex(pi) ans = 400921fb54442d18 >> hex2num('400921fb54442d19')-pi ans = 4.44089209850063e-16 As far as Matlab is concerned, .9945 does not have 4 digits, it has 53: >> sprintf('%.99g', .9945) ans = 0.99450000000000005062616992290713824331760406494140625 >> length(ans) ans = 55 There is no way within base Matlab to represent 0.9945 *exactly*. To represent 0.9945 *exactly*, you need to either use the optional Fixed Point Toolbox, or the optional Symbolic Toolbox, or the multiprecision Matlab File Exchange user contribution.
|
Next
|
Last
Pages: 1 2 Prev: anovan with random effects and continuous predictors Next: photomosaic |