# Count the number of sequential fractional *nines* of a decimal number

7 views (last 30 days)
Dimitrios Agiakatsikas on 2 May 2015
Dear all,
Does anyone know how to create a function that takes as input an array A and produces a second array with the number of sequential fractional nines for each element of array A?
For example I have an Array with the following numbers A = [0.999989023, 0.999994839, 0.999999751]
and want a function that calculates the following
B = [4, 5, 6] % A(1) has 4 nines, A(2) has 5 nines and A(3) has 6 nines.
As you understand I'm a newbie in Matlab..
Regards, Dimitris
Dimitrios Agiakatsikas on 6 May 2015
Dear John,
You are correct!. I used what you suggested.
Thank you, Dimitris

pfb on 2 May 2015
Edited: pfb on 2 May 2015
You could turn your numbers into strings, like e.g. in
st = sprintf('%1.10f',A(1))
maybe strip the trailing "0." (if they are all less than 1, in the form "0.999...")
st=st(3:end);
and then parse such string in a loop.
Or perhaps do this:
create a vector with (logical) ones in correspondence of the nines
aux1 = st=='9';
find the positions of the zeros (not nines)
i=find(~aux1)
Then the number of consecutive nines (after the decimal point) is one less than the position of the first zero (non-nine)
nnines=i(1)-1;

John D'Errico on 2 May 2015
Edited: John D'Errico on 2 May 2015
Assuming that you are doing this for numbers that fit in the VERY limited dynamic range of a double, then I would do it very simply. No real need for string processing, which will be slow.
A = [0.999989023, 0.999994839, 0.999999751]
floor(-(log10(1 - A - eps)))
ans =
4 5 6
Again, this presumes that all of the elements of A are in the open interval (0,1).
A = [0.9 0.99 0.999 0.9999 0.99999 0.999999];
floor(-(log10(1 - A - eps)))
ans =
1 2 3 4 5 6
The reason for subtracting eps there is to cater to the cases where we had something like an "exact" 0.99, which would have been internally represented only approximately.
Here are some test cases to make sure that other values do not cause a problem.
A = [0.00000999 0.09 0.1415926535 .8999999999];
floor(-(log10(1 - A - eps)))
ans =
0 0 0 0
Again, I'd be very, very careful here. Do not hope that it will succeed for the number
A = 0.99999999999999999991234;
floor(-(log10(1 - A - eps)))
ans =
15 - 2i
Double precision arithmetic has limits. In fact, I cannot be sure that one of the string schemes may not be more accurate in some extreme case. But I know that they will be slower. :)

Image Analyst on 2 May 2015

If you have the Image Processing Toolbox you can use regionprops(). First I find all the 9's, including some you missed. Then I throw out single isolated 9's like there are in A(1) and A(2).

```A = [0.999989023, 0.999994839, 0.999999751]
str = sprintf('%1.10f ', A)
nines = str == '9'
% Get lengths of stretches of all 9's of 1 or more:
measurements = regionprops(nines, 'Area');
B_all_nines = [measurements.Area]
% Throw out any that are a single 9
B_multiple_nines = B_all_nines; % Make a copy
B_multiple_nines(B_all_nines==1) = [] % Delete 1's
```

Of course you could compact that down to about 2 or 3 lines of code but I just made it super explicit so you can follow what it's doing. It shows:

```B_all_nines =
4     1     5     1     6
B_multiple_nines =
4     5     6
```

Dimitrios Agiakatsikas on 3 May 2015
Dear all,
Thank you very much for the answers. I see there is an active community here!
Regards, Dimitris
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://nl.mathworks.com/matlabcentral/answers/214741-count-the-number-of-sequential-fractional-nines-of-a-decimal-number", "fetch_time": 1675754369000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-06/segments/1674764500392.45/warc/CC-MAIN-20230207071302-20230207101302-00210.warc.gz", "warc_record_offset": 434122070, "warc_record_length": 31262, "token_count": 1063, "char_count": 3435, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2023-06", "snapshot_type": "longest", "language": "en", "language_score": 0.9141318798065186, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00025-of-00064.parquet"}