MATLAB: How to extract leading non-zero digit

leading digitMATLAB

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?

Best Answer

The answer is complicated because numbers such as 0.0006 have no exact representation in binary floating point number, and the closest number that can be represented might not have the same leading binary digit.
If one does NOT take that factor in to account, then:
y = floor(abs(x) ./ 10.^floor(log10(abs(x))));