Matlab 如何减少组合计算中的内存使用?
我有以下计划:Matlab 如何减少组合计算中的内存使用?,matlab,memory-management,Matlab,Memory Management,我有以下计划: m = 4; N = 3; a = [ones(N+m-1,1)' zeros(m,1)']; % creates an array with specified number of 0's and 1's b = perms(a); c = unique(b,'rows'); % with these last two lines, I find all possible combinations % the rest is probably not relevant fo
m = 4;
N = 3;
a = [ones(N+m-1,1)' zeros(m,1)']; % creates an array with specified number of 0's and 1's
b = perms(a);
c = unique(b,'rows'); % with these last two lines, I find all possible combinations
% the rest is probably not relevant for the question
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'));
但当我尝试m=5和N=2时,我的计算机内存不足,出现以下错误:
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);
我想我可以使用nchoosek()或combnk(),但它们没有达到我想要的效果(在一个数组中获得给定数量的1和0的所有可能的不同组合)
我可以做些什么来优化我的程序
谢谢
nchoosek
就是您要寻找的。对于结果中的每一行,nchoosek
将给出这些行的列号。该行的其余列将为零
m = 4;
N = 3;
numberOnes = N+m-1; % This is `k`
patternLength = numberOnes + m; % This is `n`
patternCount = nchoosek(patternLength, numberOnes); % Total number of combinations
bitPatterns = zeros(patternCount, patternLength); % Preallocate output matrix
patterns = nchoosek(1:patternLength, numberOnes); % Gives us the column numbers of the 1's
bitLocations = bsxfun(@(r,c) sub2ind(size(bitPatterns), r, c), % Convert the column numbers in to array indices
[1:patternCount]', patterns); % for each row in `patterns`
bitPatterns(bitLocations) = 1; % Set all of the bitLocations indices to 1
nchoosek
就是您要找的。对于结果中的每一行,nchoosek
将给出这些行的列号。该行的其余列将为零
m = 4;
N = 3;
numberOnes = N+m-1; % This is `k`
patternLength = numberOnes + m; % This is `n`
patternCount = nchoosek(patternLength, numberOnes); % Total number of combinations
bitPatterns = zeros(patternCount, patternLength); % Preallocate output matrix
patterns = nchoosek(1:patternLength, numberOnes); % Gives us the column numbers of the 1's
bitLocations = bsxfun(@(r,c) sub2ind(size(bitPatterns), r, c), % Convert the column numbers in to array indices
[1:patternCount]', patterns); % for each row in `patterns`
bitPatterns(bitLocations) = 1; % Set all of the bitLocations indices to 1
perms
使用长数组时,其大小会迅速膨胀,因此警告您不应将其用于数组中超过10个元素。优化的方法是购买比合适的sky scraper更多的RAM。@Adriaan Hehe,是的,这是一个解决方案,尽管我希望我可以使用nchoosek()或combnk()之类的东西(或类似的东西)来获得所有可能的阵列元素的不同组合,而不是先找到所有排列,然后删除所有相等的行。我不太确定你想做什么。。。您是否只是试图获取设置了特定位数的所有位字符串?这应该正是nchoosek所做的。@beaker给定一个包含N+m-1个1和m个0的数组,我想要一个矩阵,其中所有可能的1和0的组合都列为行。例如,如果我有[1 1 0],我想要[1 1 0;1 1 0 1;1 0 1 1;0 1 1 1 1 1 1]。1和0在初始数组中的顺序应该无关紧要。我希望这是有意义的。perms
在使用长数组时会迅速膨胀,因此警告您不应将其用于数组中超过10个元素。优化的方法是购买比合适的sky scraper更多的RAM。@Adriaan Hehe,是的,这是一个解决方案,尽管我希望我可以使用nchoosek()或combnk()之类的东西(或类似的东西)来获得所有可能的阵列元素的不同组合,而不是先找到所有排列,然后删除所有相等的行。我不太确定你想做什么。。。您是否只是试图获取设置了特定位数的所有位字符串?这应该正是nchoosek所做的。@beaker给定一个包含N+m-1个1和m个0的数组,我想要一个矩阵,其中所有可能的1和0的组合都列为行。例如,如果我有[1 1 0],我想要[1 1 0;1 1 0 1;1 0 1 1;0 1 1 1 1 1 1]。1和0在初始数组中的顺序应该无关紧要。我希望这是有道理的。