J中给定长度的所有布尔可能性

问题描述 投票:3回答:2

我想要最简单的动词,它给出了给定长度的所有布尔列表的列表。

EG

   f=. NB. Insert magic here

   f 2
0 0
0 1
1 0
1 1

   f 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
j
2个回答
3
投票

此功能最近已添加到stats/base插件中。

   load 'stats/base/combinatorial'  NB. or just load 'stats'
   permrep 2    NB. permutations of size 2 from 2 items with replacement
0 0
0 1
1 0
1 1
   3 permrep 2  NB. permutations of size 3 from 2 items with replacement
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
   permrep      NB. display definition of permrep
$:~ :(# #: i.@^~)

使用Qt IDE,您可以通过在Term窗口中输入permrep来查看定义open 'stats/base/combinatorial'和朋友的脚本。或者你可以view it on Github

要根据您的问题中指定定义f,以下内容应该足够:

   f=: permrep&2
   f=: (# #: i.@^~)&2  NB. alternatively
   f 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

1
投票

#:(“Antibase 2”)词汇页面有一个接近我想要的例子。我真的不明白那个原始但下面的代码给出了数字0到2 ^ n-1的基数2的数字列表:

   f=. #:@i.@(2^])

(感谢Dan让我查找#:。)

© www.soinside.com 2019 - 2024. All rights reserved.