MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中实现本福德定律 (https://www.labfans.com/bbs/showthread.php?t=23284)

poster 2019-12-10 20:30

如何在MATLAB中实现本福德定律
 
我想实现一个本福德定律( [URL]http://en.wikipedia.org/wiki/Benford%27s_law[/URL] )版本,该版本基本上要求数字的第一位数字来进行分布分析。

1934---> 1 0.04 ---> 4 -56 ---> 5 您如何在MATLAB中执行此操作?



[B]回答:[/B]

您可以通过几种方法执行此操作...
[LIST][*]使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html"]REGEXP[/URL] :

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[*]使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ismember.html"]ISMEMBER[/URL] :

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[/LIST][B]编辑:[/B]

[URL="http://blogs.mathworks.com/videos/2010/04/08/puzzler-benfords-law/"]在MathWorks的一个博客[/URL]上已经对此主题进行了一些讨论。那里提供了一些有趣的其他解决方案。提出的一个问题是矢量化解决方案,因此我想出了一个矢量化版本:

numberVector = [1934 0.04 -56]; numberStrings = cellstr(num2str(numberVector(:),16)); firstIndices = regexp(numberStrings,'[1-9]','once'); firstNumbers = cellfun(@(s,i) s(i),numberStrings,firstIndices);

[url=https://stackoverflow.com/questions/2602365]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:01

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.