# Huge matrix with Nchoosek

23 views (last 30 days)
Hamad Gul on 13 Sep 2019
Commented: Vahid Hamdipoor on 23 Mar 2020
I have a program where I need all possible combinations between two variables(N and T). Facing "Out of Memory" problem whenever T increases 4. Is there any solution for this?
clear;
% spiky signal generation
N = 255; %maximum users
T = 10; % number of spikes
a = zeros(N,1); %creating N-by-1 all zero vector
C = nchoosek(1:N,T); %creating all the combination values between N and T
e= nchoosek(N,T); %number of all possible combinations
x0 = zeros(N,1); %creating N-by-1 all zero vector
count = 0; %used in calculating success
%disp(C);
for K = 1:1:N %K, m are two variables to identify the element in C
a = zeros(N,1); %creating N-by-1 all zero vector
for m = 1:1:T; %used to initiate loop for reading the elements in C
i= C(K,m);
a(i) = 1; %i defining the place of 1 in all zero vector a
x0(1:N)=a; %putting x0 as a
end
if a(i) ~= 0; %implementing network conditions
n = randn(1,1); % white gaussian noise, 0dB variance
h = 1/sqrt(2)*[randn(1,1) + j*randn(1,1)]; % Rayleigh channel
h_abs = abs(h);
x0(i) = 1 * h_abs + 10^(-1/1)*n;
y = HD2255*x0; % measurements with no noise
lambda = 0.0001; % regularization parameter
rel_tol = 0.01; % relative target duality gap actual 0.01
[x,status]=l1_ls(HD2255,y,lambda,rel_tol);
for s = 1:size(x0,1); %starting a loop to calculate success
for t = 1:size(x0,2);
if x0(s,t) ~= 0; %when x0 is avtive
d(s,t) = abs(x0(s,t)) - abs(x(s,t)); %difference
if d(s,t) < 0.1; %success criteria
count = count + 1; %counting the number of times it is successful
% cnt = count / e
%perc = (cnt*100)/T
% sta(s,t) = -1;
%else sta(s,t) = 1;
%fprintf('count\n', count)
disp(count);
end
end
end
end
end
end
%end
cnt = count / e
perc = (cnt*100)/T

John D'Errico on 16 Sep 2019
@Hamad - you insult the people who gave you much time to answer your question, by making this question of no value to anyone else to ever read. You also make it less likely that those same people will bother to answer your future questions, by your action.
Hamad Gul on 16 Sep 2019
I am sorry but that was not my intention. You are right I shouldn't have cleared the question. I didn't understand how this page works but now I will restore it to make it helpful for future.
and for Stephen Cobeldick and Bruno Luong, by future I mean near future...
Not the future after 84 million years.
Rena Berman on 19 Sep 2019

Bruno Luong on 13 Sep 2019
Edited: Bruno Luong on 13 Sep 2019
You became real "Patient" now? Good! Then run this look at the screen and wait until it stops. If your type Ctrl C or kill MATLAB or reboot your PC, etc.., you must come back and change your claim ;-)
seq_nchoosek(255,10); % function bellow
while true
c = seq_nchoosek(); % function bellow
disp(c)
if size(c,1)==0
break
end
% ... do your stuff with c
end
(Put function in seq_nchoosek.m mfile)
function v = seq_nchoosek(n,k)
% Sequential NCHOOSEK
% N and K non negative integers k <= n
% seq_nchoosek(n,k) % reset the sequence of combination
% v = seq_nchoosek() % query the next combination
%
% % Exemple of calling sequence:
% seq_nchoosek(5,3)
% while true
% c = seq_nchoosek()
% if size(c,1)==0
% break
% end
% % ... do something with c
% end
%
persistent V S I K
if nargin >= 2
% reset
if ~(isscalar(n) && isscalar(k))
error('seq_nchoosek: n and k must be scalars');
end
if k > n
error('seq_nchoosek: k must be smaller or equal to n');
end
if n == 0
V = [];
else
k = floor(k);
n = floor(n);
if n < 0 || k < 0
error('seq_nchoosek: n and k must non negative integers');
end
V = 1:k;
I = k;
K = k;
S = V+(n-k);
end
end
if nargout >= 1
% query
v = V;
if size(V,1) > 0
% move to next
I = find(V-S,1,'last');
if ~isempty(I)
V(I) = V(I)+1;
V(I+1:K) = V(I)+(1:K-I);
else
V = [];
end
end
end
end

Stephen Cobeldick on 13 Sep 2019
"I will try this now and will post the result here"
Our great-great-great-great-...-great grandchildren are looking forward to reading your post in about 80 million years or so.
madhan ravi on 13 Sep 2019
Probably this story will be read in an another planet.
Vahid Hamdipoor on 23 Mar 2020
@Bruno Luong Thank you so much. This is a great piece of code, And it solved my problem as well.