Combinations and memory uses

v = uint16([1 10 11 12 13 14 16 18 21 22 23 24 26 32 35 36 37 39 41 42 44 46 56 57 58 59 62 66 68 69 71 72 73 77 80]);
C = nchoosek(v,uint16(22))
I try this code for combination but use memory in matlab is very huge and take time
Can you help me?

 Accepted Answer

Why do people never think about what they wish to do? Computers are not infinite in size, nor are they all powerful except on tv.
v = [1 10 11 12 13 14 16 18 21 22 23 24 26 32 35 36 37 39 41 42 44 46 56 57 58 59 62 66 68 69 71 72 73 77 80];
numel(v)
ans =
35
nchoosek(35,22)
ans =
1.4763e+09
So your call to nchoosek will tell MATLAB to create an array of size: 1.5e9 by 22.
Even as uint16, that matrix will require 2 bytes per element.
1.5e9*22*2
ans =
6.6e+10
So 66 gigabytes of RAM, just to create that matrix. It might take some time to do, even if you have close to that much RAM. Most likely, your disk started swapping like mad.
Computers do have finite capabilities.

2 Comments

Thank you John :)
Using the duration functionality we can also tell how long it would take to operate on that data at a rate of one combination processed per second.
years(seconds(nchoosek(35, 22)))
The SECONDS call creates a duration representing a number of seconds, and the YEARS call converts that duration into the equivalent number of exact years.
At a little over 46 3/4 years you might want to rethink your approach, see if you can parallelize your operation, or wait a little while and see if Moore's law continues to hold.

Sign in to comment.

More Answers (1)

Jan
Jan on 21 Sep 2015
Edited: Jan on 21 Sep 2015
If the values of your vector are smaller than 256, you can convert them in the type uint8 and use FEX: vchookek.mex . Matlabs nchoosek used double values, at least in older Matlab versions. Reducing the tapy of the data as far as possible might allow to squeeze the data into your RAM.
There are many other useful submissions in the FileExchange, e.g. FEX:a-fast-nchoosek-alternative-for-engineers-who-like-fast-nchoosek-alternatives

2 Comments

I couldn't use this code, because I don't know how call this function Jan
See "help vchoosek" for detailed instructions. Either compile the code or download the precomiled file from the provided link. Then call this function exactly as you would call Matlab's nchoosek.

Sign in to comment.

Categories

Asked:

on 21 Sep 2015

Commented:

Jan
on 29 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!