创建给定长度的所有多索引
给定一个整数 k
和一个尺寸 d
,我如何使用 length(alpha)= d
alpha /code>和 sum(alpha)= k
in R?
示例:对于 k = 3
和 d = 2
,如果我们在列表中组织了多名 alphas
,我们会得到
alphas[[1]] = c(3,0)
alphas[[2]] = c(2,1)
alphas[[3]] = c(1,2)
alphas[[4]] = c(0,3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于描述,我们可以在
rep
许可list> list
'之后使用expand.grid
在“ 0”到'k'的序列上使用' d'times,然后过滤
仅具有sum
为'k'检测
或更快的行是用
rowsums
过滤速度稍快地也是基于约束的组合功能在
rcppalgos
-testing
中或@josephwood提到的
permuteGeneral
与combogeneral
或
使用
构图
Based on the description, we may use
expand.grid
on the sequence from '0' to 'k', afterrep
licating thelist
'd' times, thenFilter
only the rows having thesum
as 'k'-testing
Or slightly more faster would be to filter with
rowSums
There is also a constraints based combinations functions in
RcppAlgos
-testing
Or as @JosephWood mentioned
permuteGeneral
would be more adequate compared tocomboGeneral
Or with
compositions