对的排列列表 [英] permutation list of pairs

查看:84
本文介绍了对的排列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,对你们许多人来说,这很简单,这将挽救我的生活:我需要生成一系列数字的所有对的置换集.例如,对于1:6,它将得到30个子集,即n(n-1):

An easy one for many of you, I'm sure, which will save my day : I need to generate a permutation set of all the pairs of a sequence of numbers. For example, for 1:6, it will give as a final result, 30 subsets, i.e. n(n-1) :

(1,2),(3,4),(5,6)
...
(1,6),(2,3),(4,5)

我需要一对,而不是一对,所以(3,4)和(4,3)是唯一的一对.

I need pairs, not couples, so that (3,4) and (4,3) is an unique pair.

combn(1:6,2)给了我一张表格,其中以配对为列,但是如何从中产生配对列表呢?

combn(1:6,2) gives me a table with my pairs as columns, but how do I produce my list of pairs out of it?

combn(1:6,2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,]    1    1    1    1    1    2    2    2    2     3     3     3     4     4     5
[2,]    2    3    4    5    6    3    4    5    6     4     5     6     5     6     6

谢谢

推荐答案

我们可以在 combn 中设置参数 simplify = FALSE ,使其返回列表:

We can set the argument simplify = FALSE in combn such that it returns a list:

combn(1:6, 2, simplify = FALSE)
#[[1]]
#[1] 1 2
#
#[[2]]
#[1] 1 3
#
#[[3]]
#[1] 1 4
#
#[[4]]
#[1] 1 5
#...

这篇关于对的排列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆