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

조회 수: 3 (최근 30일)
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
  댓글 수: 4
John D'Errico
John D'Errico 2015년 5월 3일
Well, if that is what you want, then my solution gives it to you almost exactly! Just remove the floor, and it is exact.
-log10(1-A)
ans =
4.9595 5.2873 6.6038
Dimitrios Agiakatsikas
Dimitrios Agiakatsikas 2015년 5월 6일
Dear John,
You are correct!. I used what you suggested.
Thank you, Dimitris

댓글을 달려면 로그인하십시오.

채택된 답변

pfb
pfb 2015년 5월 2일
편집: pfb 2015년 5월 2일
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;

추가 답변 (3개)

John D'Errico
John D'Errico 2015년 5월 2일
편집: John D'Errico 2015년 5월 2일
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
Image Analyst 2015년 5월 2일

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
Dimitrios Agiakatsikas 2015년 5월 3일
Dear all,
Thank you very much for the answers. I see there is an active community here!
Regards, Dimitris

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by