ram issue while generating subsets

1 view (last 30 days)
sina
sina on 10 Jul 2013
alright, I wrote a code in matlab which in a part of that it needs to generate all subsets of a vector and process all of them one by one. For example having vector X, I first used the function combntns(X, k) to generate the subsets but there is a problem that X can be in a very high dimension so to save the all subsets of X it requires massive amount of ram! And I always run out of ram memory while running the code. That is so unnecessary to save all the subsets since the algorithm will process them one by one. I wonder is there any function to do this for me or I have to write a code manually to do so?
  2 Comments
Matt J
Matt J on 10 Jul 2013
Edited: Matt J on 10 Jul 2013
I think the path forward is to explain why you think you need ALL subsets, so that we can assess whether that is true or not. It sounds like a brute force approach that's probably meant to be done differently, maybe with a few compromises.
sina
sina on 10 Jul 2013
ok, I don't need ALL subsets I have a limit for subset size for example ALL subsets with length 3

Sign in to comment.

Accepted Answer

sina
sina on 10 Jul 2013
Edited: sina on 10 Jul 2013
I just used this code I wrote, in case anyone faced this problem in future.
function nextSubset = getNextSubset(set, currentSubset)
% this function returns indexes of next subset
% subsets will return with their length order
% returns NaN if end of subsets reached
if exist('currentSubset', 'var') == 0
nextSubset = [];
return
end
if isempty(currentSubset)
nextSubset = 1;
return
end
n = length(set);
m = length(currentSubset);
if currentSubset == n-m+1:n
if m == n
nextSubset = NaN;
return
end
nextSubset = 1:m+1;
return
end
nextSubset = currentSubset;
nextSubset(m) = nextSubset(m) + 1;
if nextSubset(m) == n+1
c = m - 1;
while nextSubset(c) == n-m+c
c = c - 1;
if c == 0
nextSubset = NaN;
return
end
end
nextSubset(c:m) = nextSubset(c)+1:m-c+nextSubset(c)+1;
end
end

More Answers (1)

Matt J
Matt J on 10 Jul 2013
Edited: Matt J on 10 Jul 2013
ok, I don't need ALL subsets I have a limit for subset size for example ALL subsets with length 3
If the length of the subset is small (typically around 3 if that's what you're saying), then nchoosek should give you what you're looking for pretty easily and without too much strain in memory,
Xsubsets=nchoosek(X,3);
  1 Comment
sina
sina on 10 Jul 2013
nop, it has the the previous problem too. I just didn't want to save the subset all at once to prevent occupying memory. The code I wrote manually does exactly what is needed to be done.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!