Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Benford's law in MATLAB

I want to implement a version of Benford's law (http://en.wikipedia.org/wiki/Benford%27s_law) that basically asks for the first digit of a number to do analysis on the distribution.

1934---> 1
0.04 ---> 4
-56 ---> 5

How do you do this in MATLAB?

like image 576
user312141 Avatar asked Feb 02 '26 06:02

user312141


1 Answers

function res = first_digit(number)
    number = abs(number);
    res = floor(number / (10 ^ floor(log10(number))));
end

It works for all real numbers (see gnovice's comment for an extreme case)

like image 195
yassin Avatar answered Feb 04 '26 21:02

yassin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!