Huge matrix with Nchoosek

조회 수: 5 (최근 30일)
Hamad Gul
Hamad Gul 2019년 9월 13일
댓글: Vahid Hamdipoor 2020년 3월 23일
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
  댓글 수: 13
Hamad Gul
Hamad Gul 2019년 9월 16일
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
Rena Berman 2019년 9월 19일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (1개)

Bruno Luong
Bruno Luong 2019년 9월 13일
편집: Bruno Luong 2019년 9월 13일
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
  댓글 수: 5
madhan ravi
madhan ravi 2019년 9월 13일
Probably this story will be read in an another planet.
Vahid Hamdipoor
Vahid Hamdipoor 2020년 3월 23일
@Bruno Luong Thank you so much. This is a great piece of code, And it solved my problem as well.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by