How can I reduce memory-use on this combinatorics computation?
1 view (last 30 days)
Show older comments
I have the following program:
m = 4;
N = 3;
a = [ones(N+m-1,1)' zeros(m,1)'];
b = perms(a);
c = unique(b,'rows');
d = [ones(length(c),1) c ones(length(c),1)];
a_powers = zeros(length(d(:,1)),1);
for i = 1:length(d(:,1))
a_powers(i) = nnz(diff(d(i,:))==0);
end
[n_terms,which_power]=hist(a_powers',unique(a_powers'));
But my computer runs out of memory when I try m=5 and N=2, with the following error:
Out of memory. Type HELP MEMORY for your options.
Error in perms>permsr (line 53)
P = V(P);
Error in perms (line 26)
P = permsr(V);
I thought I could use nchoosek() or combnk(), but they didn't do what I wanted (to get all possible different combinations of a given number of ones and zeros in an array).
What can I do to optimize my program?
Thanks!
0 Comments
Answers (2)
John D'Errico
on 27 Aug 2016
clear
m = 5;
N = 2;
a = [ones(N+m-1,1)' zeros(m,1)'];
whos a
Name Size Bytes Class Attributes
a 1x11 88 double
factorial(11)
ans =
39916800
factorial(11)*11*8
ans =
3.5127e+09
So you need at least 3.5 gigabytes of CONTIGUOUS FREE RAM to call perms here. Usually, you will need several times that, because temporary copies of arrays may be made along the way.
It is easy to generate small looking problems that are not in fact small, and your next question will surely be why your code exceeds available memory when m=6 and how to fix it.
Stephen23
on 29 Aug 2016
Don't calculate all permutations at once. Use nchoosek or perhaps one of the submissions on MATLAB File Exchange:
0 Comments
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!