How to extract leading non-zero digit?
Show older comments
I'm working on a research problem related to Benford's Law which states that the distribution of leading digits is not random. This is probably because many things grow logarithmically. I am trying to extract the leading digit from these vectors below:
10 --> 1
13 --> 1
0.3 --> 3
-4 --> 4
-5 --> 5
-0.006 --> 6
Input will be a vector
x = [1 0.3 -2 0.001 -0.0006, 582398, 3020];
Output should be
y = [1 3 2 1 6 5 3];
Any help?
Accepted Answer
More Answers (3)
Ned Gulley
on 11 Jan 2011
You'll probably need to do some kind of textual manipulation. Here's one way to do it.
function y = leadingDigit(x)
s = sprintf('%1.2e\n',abs(x));
y = s(1:(length(s)/length(x)):end)-48;
end
Daniel Shub
on 28 Jun 2012
2 votes
Is this really the first question on Answers? I was going to use our new magic power and start accepting answers. The only problem is I think there is now a better answer to this question on Loren's blog.
1 Comment
Jan
on 28 Jun 2012
I thought of accepting this answer, because it contains a link to good solutions. But actually the method shown by Walter hits the point (and is found in this blog also). Ned's method is more efficient than STR2DOUBLE, but a clean numerical approach is moire direct for a numerical question.
Oleg Komarov
on 5 Sep 2012
regexp(num2str(x), '(?<=(^|\s+)[\-\.0]*)[1-9](?=[\d\.]*)', 'match')
1 Comment
Asif Newaz
on 22 Nov 2019
can u explain the 'expression'... i've found regexp quite complicated but very useful
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!