如何生成组合矩阵
我有 5 个项目,每个项目的值为 1 或 -1。我想生成一个由可能组合的行组成的矩阵。项目的顺序并不重要,组合的顺序也不重要。我知道我可以机械地完成此操作,但我认为必须有人知道生成此矩阵的快捷方式。如果这与其他问题类似,我深表歉意,但我找到的解决方案都不能以我的编程技能应用于这个特定问题。
I have 5 items each of which can take on the value of 1 or -1. I want to generate a matrix that consists of rows of the possible combinations. The order of the items does not matter and the order of the combinations does not matter. I know I could do this mechanically, but I thought that someone must know a shortcut to generating this matrix. I apologize if this is similar to other questions but none of the solutions I have found can be applied to this particular problem with my programming skills.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
概括格雷格的答案:
To generalize Greg's answer:
与
expand.grid
相比,data.table
包的替代方案稍快一些:Alternative from
data.table
package is slightly faster compared toexpand.grid
: