【问题标题】:Permutations of 0s and 1s with Recursion使用递归的 0 和 1 的排列
【发布时间】:2011-06-17 20:46:03
【问题描述】:

我正在尝试使用递归编写 0 和 1 的所有 6 位排列(即 [0 0 0 0 0 1]、[0 0 0 0 1 0]、[0 0 0 0 1 1]、 [0 0 0 1 0 0],... [1 1 1 1 1 1])。我正在遵循我从 Yahoo! Answers 获得的提示,我得到了以下提示,但它并没有一直走到最后,并且有重复的条目。

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

【问题讨论】:

标签: matlab recursion binary permutation


【解决方案1】:

只需从 0 迭代到 26-1 并调用 Matlab 的 dec2bin(...) 函数。您可以使用sprintf(...) 将结果用零填充。

【讨论】:

  • 啊,谢谢!这比我认为我需要的要容易得多。
【解决方案2】:

如果您想计算 6 位数字的所有可能值,有比使用递归更有效的方法。使用函数DEC2BIN 是一种选择,但this previous question 表明使用函数BITGET 或通过查找表进行索引的实现要快得多。例如,这是一种可能的解决方案:

allperms = cell2mat(arrayfun(@(b) {bitget((0:2^6-1).',b)},6:-1:1));

然后您可以将 converter 函数逐行应用于结果:

converter = @(b) (1-2.*b(:,1)).*...             %# The sign contribution
            2.^(b(:,2:3)*[2; 1] - 1).*...       %# The exponent contribution
            (1 + b(:,4:6)*[0.125; 0.25; 0.5]);  %# The fraction contribution
values = converter(allperms);

并且可以如下显示结果:

>> disp(sprintf('%d %d %d %d %d %d : %f\n',[allperms values].'))
0 0 0 0 0 0 : 0.500000
0 0 0 0 0 1 : 0.750000
0 0 0 0 1 0 : 0.625000
0 0 0 0 1 1 : 0.875000
0 0 0 1 0 0 : 0.562500
0 0 0 1 0 1 : 0.812500
0 0 0 1 1 0 : 0.687500
0 0 0 1 1 1 : 0.937500
0 0 1 0 0 0 : 1.000000
0 0 1 0 0 1 : 1.500000
0 0 1 0 1 0 : 1.250000
0 0 1 0 1 1 : 1.750000
0 0 1 1 0 0 : 1.125000
0 0 1 1 0 1 : 1.625000
0 0 1 1 1 0 : 1.375000
0 0 1 1 1 1 : 1.875000
0 1 0 0 0 0 : 2.000000
0 1 0 0 0 1 : 3.000000
0 1 0 0 1 0 : 2.500000
0 1 0 0 1 1 : 3.500000
0 1 0 1 0 0 : 2.250000
0 1 0 1 0 1 : 3.250000
0 1 0 1 1 0 : 2.750000
0 1 0 1 1 1 : 3.750000
0 1 1 0 0 0 : 4.000000
0 1 1 0 0 1 : 6.000000
0 1 1 0 1 0 : 5.000000
0 1 1 0 1 1 : 7.000000
0 1 1 1 0 0 : 4.500000
0 1 1 1 0 1 : 6.500000
0 1 1 1 1 0 : 5.500000
0 1 1 1 1 1 : 7.500000
1 0 0 0 0 0 : -0.500000
1 0 0 0 0 1 : -0.750000
1 0 0 0 1 0 : -0.625000
1 0 0 0 1 1 : -0.875000
1 0 0 1 0 0 : -0.562500
1 0 0 1 0 1 : -0.812500
1 0 0 1 1 0 : -0.687500
1 0 0 1 1 1 : -0.937500
1 0 1 0 0 0 : -1.000000
1 0 1 0 0 1 : -1.500000
1 0 1 0 1 0 : -1.250000
1 0 1 0 1 1 : -1.750000
1 0 1 1 0 0 : -1.125000
1 0 1 1 0 1 : -1.625000
1 0 1 1 1 0 : -1.375000
1 0 1 1 1 1 : -1.875000
1 1 0 0 0 0 : -2.000000
1 1 0 0 0 1 : -3.000000
1 1 0 0 1 0 : -2.500000
1 1 0 0 1 1 : -3.500000
1 1 0 1 0 0 : -2.250000
1 1 0 1 0 1 : -3.250000
1 1 0 1 1 0 : -2.750000
1 1 0 1 1 1 : -3.750000
1 1 1 0 0 0 : -4.000000
1 1 1 0 0 1 : -6.000000
1 1 1 0 1 0 : -5.000000
1 1 1 0 1 1 : -7.000000
1 1 1 1 0 0 : -4.500000
1 1 1 1 0 1 : -6.500000
1 1 1 1 1 0 : -5.500000
1 1 1 1 1 1 : -7.500000

【讨论】:

  • 实际上,矢量化的dec2bin 似乎比这里介绍的bitget 解决方案更快。试试:x = dec2bin(1:2^6-1); allperms = x-'0';
【解决方案3】:

如果您想以简单的方式编写所有排列,请使用dec2bin 获取二进制值,如@Bart 推荐。

但是,如果您使用矢量化,这种操作通常可以显着提高效率。

因此,不要遍历所有值并逐个获取结果,只需执行以下操作:

x = dec2bin(1:2^6-1);

如果您希望输出为矩阵,也可以这样做:

x = x - '0';

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多