23 views (last 30 days)

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;

load('HD2255.mat');

% 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

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

%

% See also: NCHOOSEK

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.

Vahid Hamdipoor
on 23 Mar 2020

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 13 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745468

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745468

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745474

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745474

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745477

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745477

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745483

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745483

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745484

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745484

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745497

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745497

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745498

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_745498

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746309

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746309

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746320

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746320

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746322

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746322

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746363

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746363

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746371

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_746371

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_747724

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/480216-huge-matrix-with-nchoosek#comment_747724

Sign in to comment.