# Lookup-scheme for potentially large n choose k matrices

4 views (last 30 days)
Ulrik William Nash on 10 Dec 2019
Commented: Rik on 10 Dec 2019
I am writing a simulation that includes the variables n and k. These variable serves a part in numerous "n choose k" calculations. Since these caluclations are often identical in my simulation, and since n and k can be large (sometimes n > 20), I want to implement a scheme whereby the "n choose k" calculations are pre-computed and stored for lookup.
More specifically, k is not constant throughout my simulation. Within a simulation it can, for example, change from 5 to 4, to 6, to another number. Now, I want to avoid adding IF conditions for every value if n and k but I am unsure about the best-practice for creating a look-up scheme in my situation. To be sure, what must be looked-up are the "n choose k" matrixes, and not simply the binomial coefficients.
How might the described scheme be implemented?

Rik on 10 Dec 2019
Edited: Rik on 10 Dec 2019
If the citeria below are satisfied, you can use memoize:
• Performance is important.
• The function is time consuming.
• The function has return values that are determined entirely by the input values, and has no side effects.
• System memory is adequate to store unique input and output combinations.
The last requirement is probably not true in your case, so you need to find a way to store the matrices on disk.
You could store it in a mat file that you would load partially, but I think a cleaner solution would be to use a preference group, so getpref and setpref. Note that this is a slow method for smaller sizes of n and k, because it loads data from the disk. You can probably improve on this by determining values where it is faster to memoize or generate on the fly, and values where loading from disk may be faster.
%n must be a scalar, which is expanded to 1:n
prefname=sprintf('n_%d_k_%d',n,k);
else
%create and store to disk
mat=nchoosek(1:n,k);
end
end
If you no longer have any use for this function (or if disk space becomes an issue), you can remove the entire preference group all at once: