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

我正在尝试使用递归(即[0 0 0 0 0 0 1 1,[0 0 0 0 1 0],[0 0 0 0 1 1],[0 0]写所有0和1s的6位排列0 1 0 0],... [1 1 1 1 1 1])。我正在跟踪从雅虎获得的提示!答案我有以下内容,但它并没有一直到最后,并且有重复的条目。

function [c] = myIEEEBaby_all_decimals(h, n) % n: number of characters more to add if n == 0 converter(h); return end in1 = [h 1]; in2 = [h 0]; converter(in1); converter(in2); if length(h) < n myIEEEBaby_all_decimals([in1], n-1) myIEEEBaby_all_decimals([in2], n-1) end end function [d] = converter(IEEE) % convert custom IEEE representation to decimal IEEE = [zeros(1,6-length(IEEE)), IEEE]; s = IEEE(1); characteristic = IEEE([2,3]); k = find(fliplr(characteristic)) - 1; c = sum(2.^k); fraction = IEEE([4:6]); f = sum(2.^-(find(fliplr(fraction)))); d = (-1)^s*2^(c-1)*(1+f); disp([num2str(IEEE),' : ', num2str(d)]); end 输出(MATLAB)只是:

>> myIEEEBaby_all_decimals([],6) 0 0 0 0 0 1 : 0.75 0 0 0 0 0 0 : 0.5 0 0 0 0 1 1 : 0.875 0 0 0 0 1 0 : 0.625 0 0 0 1 1 1 : 0.9375 0 0 0 1 1 0 : 0.6875 0 0 1 1 1 1 : 1.875 0 0 1 1 1 0 : 1.375 0 0 1 1 0 1 : 1.625 0 0 1 1 0 0 : 1.125 0 0 0 1 0 1 : 0.8125 0 0 0 1 0 0 : 0.5625 0 0 1 0 1 1 : 1.75 0 0 1 0 1 0 : 1.25 0 0 1 0 0 1 : 1.5 0 0 1 0 0 0 : 1 0 0 0 0 0 1 : 0.75 0 0 0 0 0 0 : 0.5 0 0 0 0 1 1 : 0.875 0 0 0 0 1 0 : 0.625 0 0 0 1 1 1 : 0.9375 0 0 0 1 1 0 : 0.6875 0 0 0 1 0 1 : 0.8125 0 0 0 1 0 0 : 0.5625 0 0 0 0 0 1 : 0.75 0 0 0 0 0 0 : 0.5 0 0 0 0 1 1 : 0.875 0 0 0 0 1 0 : 0.625 0 0 0 0 0 1 : 0.75 0 0 0 0 0 0 : 0.5

回答:

简单地从0迭代到2 6 -1并调用Matlab的dec2bin(...)函数。您可以使用sprintf(...)将结果填充零。



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

主题工具
显示模式

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

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



所有时间均为北京时间。现在的时间是 23:43


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