Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:30   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何在MATLAB中实现本福德定律

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

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



回答:

您可以通过几种方法执行此操作...
  • 使用REGEXP

    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);

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



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


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