Matlab nchoosek问题 [英] Matlab nchoosek problem

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

问题描述

我的问题与Matlab有关.存在一个名为nchoosek([vector],integer)的函数.通过使用此功能,我想获得给定向量的所有2元素组合. (即nchoosek([1:10000,2])).如Matlab文档中所述,这非常慢.

My question is Matlab related. There exist a fnct named nchoosek([vector],integer). By using this function I would like to get all 2-elements combinations of the given vector. (i.e nchoosek([1:10000, 2])). This is very slow, as stated in matlab documentation.

问题是:是否有更快的方法来完成相同的工作?".

The question is : "Is there a faster way to do the same job?".

感谢您的宝贵时间,我非常感谢您的努力.

Thank you for your time I really appreciate your efforts.

推荐答案

如果仅需要2个元素的组合,则可以使用

If it's only 2-element combinations you need, you can use NDGRID. Note that all two-element combinations up to N require N^2 values, so if Matlab starts paging, the process will be slow.

N = 100;
[xx,yy] = ndgrid(1:N,1:N);
allCombinations = [xx(:),yy(:)];

请注意,函数 NDGRID nchoosek . 前者返回具有所有可能的N ^ 2组合的双行向量,而后者则省略了两次相同元素的组合,以及仅改变顺序的组合.导致仅(N ^ 2-N)/2个行元素.

Please be aware that the function NDGRID differs significantly from nchoosek. The former returns a double-row vector with all possible N^2 combinations, while the latter is leaving out combinations of two times the same element, as well as combinations where just the order is changed. Leading to just (N^2-N)/2 row elements.

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

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