0
我有以下程序:如何減少此組合計算中的內存使用量?
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的組合)。
如何優化我的程序?
謝謝!
'perms'會因爲冗長的數組而迅速爆發,因此警告您不應該在數組中使用超過10個元素。優化的方式是購買更多的內存,而不是放在適當的天空刮板中。 – Adriaan
@Adriaan呵呵,是的,這是一個解決方案,雖然我希望我可以使用像nchoosek()或combnk()(或類似的東西)來獲得數組元素的所有可能的不同組合,而不是首先查找所有的排列組合然後刪除所有相等的行。 –
我不太確定你想要做什麼...你只是試圖讓所有位串具有特定數量的位集?這應該是'nchoosek'所做的。 – beaker