Matlab nchoosek problem

My question is related to Matlab. There is an fnct named nchoosek ([vector], integer). Using this function, I would like to get all combinations of two elements of this vector. (i.e. nchoosek ([1: 10000, 2])). This is very slow as indicated in the Matlab documentation.

Question: "Is there a faster way to do the same job?".

Thanks for your time, I really appreciate your efforts.

+3
source share
1 answer

If you need only 2-element combinations, you can use NDGRID . Please note that all two-element combinations up Nrequire values N^2, 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 .

+3

Source: https://habr.com/ru/post/1794697/


All Articles