简体   繁体   中英

Matlab nchoosek problem

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. (ie 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.

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(:)];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM