3

我想实现一个版本的本福德定律(http://en.wikipedia.org/wiki/Benford%27s_law),它基本上要求一个数字的第一个数字来对分布进行分析。

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

你如何在 MATLAB 中做到这一点?

4

4 回答 4

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

它适用于所有实数(有关极端情况,请参阅 gnovice 的评论)

于 2010-04-08T18:33:52.010 回答
2

有几种方法可以做到这一点...

  • 使用正则表达式

    wholeNumber = 1934;                      %# Your number
    numberString = num2str(wholeNumber,16);  %# Convert to a string
    matches = regexp(numberString,'[1-9]','match');  %# Find matches
    firstNumber = str2double(matches{1});  %# Convert the first match to a double
    
  • 使用ISMEMBER

    wholeNumber = 0.04;                      %# Your number
    numberString = num2str(wholeNumber,16);  %# Convert to a string
    isInSet = ismember(numberString,'123456789');  %# Find numbers that are
                                                   %#  between 1 and 9
    numberIndex = find(isInSet,1);           %# Get the first number index
    firstNumber = str2double(numberString(numberIndex));  %# Convert to a double
    

编辑:

一个MathWorks 博客上已经出现了关于这个主题的一些讨论。那里提供了一些有趣的附加解决方案。提出的一个问题是矢量化解决方案,所以这是我想出的一个矢量化版本:

numberVector = [1934 0.04 -56];
numberStrings = cellstr(num2str(numberVector(:),16));
firstIndices = regexp(numberStrings,'[1-9]','once');
firstNumbers = cellfun(@(s,i) s(i),numberStrings,firstIndices);
于 2010-04-08T18:22:10.280 回答
1

使用 log10 和 floor 内置函数,

floor(x./10.^floor(log10(x)))

returns the first digit of all elements in an array as well.

于 2010-04-08T18:38:26.173 回答
0

Let me add another string-based solution (vectorized as well):

FirstDigit = @(n) sscanf(num2str(abs(n(:)),'%e'), '%1d', numel(n));

and tested on the cases mentioned here:

>> FirstDigit( [1934 0.04 -56 eps(realmin)] )
ans =
     1
     4
     5
     4
于 2011-10-20T22:48:23.343 回答