|
From: Daniel on 6 May 2008 23:20 Hello, I'm looking for a fast method of finding all of the unique combinations of sets of elements (assuming that all sets are the same). To give an example by contrast, the Matlab File Exchange file 'allcomb' generates all unique combinations of sets of elements assuming taht all the sets are different), so: allcomb([1:3], [1:3]) ans = 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 what I want is: ans = 1 1 1 2 1 3 2 2 2 3 3 3 A short script which achieves this is (where hyps is the outcome of interest): x = allcomb([1:3], [1:3]); hyps = x(1,:); for i = 2:size(x, 1) t = x(i,:); test = perms(t); for j = 1:size(test,1) tr(j) = any(all(repmat(test(j,:),... size(hyps,1), 1) == hyps,2)); end if sum(tr) == 0 hyps = [hyps; x(i,:)]; end end However, this process is painfully slow for large sets (e.g., [1:900], [1:900], [1:2] takes 6 days to complete!). Any advice on how to speed this process up or direction towards an existing function would be greatly appreciated. Thanks, Dan
From: helper on 6 May 2008 23:51 "Daniel " <danielDOTrDOTlittle(a)gmail.com> wrote in message <fvr755$inh$1(a)fred.mathworks.com>... > Hello, > > I'm looking for a fast method of finding all of the unique > combinations of sets of elements (assuming that all sets > are the same). > > To give an example by contrast, the Matlab File Exchange > file 'allcomb' generates all unique combinations of sets of > elements assuming taht all the sets are different), so: > > allcomb([1:3], [1:3]) > > ans = > > 1 1 > 1 2 > 1 3 > 2 1 > 2 2 > 2 3 > 3 1 > 3 2 > 3 3 > > what I want is: > > ans = > > 1 1 > 1 2 > 1 3 > 2 2 > 2 3 > 3 3 > > A short script which achieves this is (where hyps is the > outcome of interest): > > x = allcomb([1:3], [1:3]); > > hyps = x(1,:); > for i = 2:size(x, 1) > t = x(i,:); > test = perms(t); > for j = 1:size(test,1) > tr(j) = any(all(repmat(test(j,:),... > size(hyps,1), 1) == hyps,2)); > end > if sum(tr) == 0 > hyps = [hyps; x(i,:)]; > end > end > > However, this process is painfully slow for large sets > (e.g., [1:900], [1:900], [1:2] takes 6 days to complete!). > > Any advice on how to speed this process up or direction > towards an existing function would be greatly appreciated. > > Thanks, > > Dan You are looking for all combinations with replacement. Check out the file COMBSREP.m at the following link: http://www.mathworks.com/support/solutions/files/s36265/ combsrep(1:3, 2)
From: Roger Stafford on 7 May 2008 00:08 "Daniel " <danielDOTrDOTlittle(a)gmail.com> wrote in message <fvr755$inh $1(a)fred.mathworks.com>... > Hello, > I'm looking for a fast method of finding all of the unique > combinations of sets of elements (assuming that all sets > are the same). > ....... > Dan ------- I believe the call c = nchoosek(1:n,k); does just what you want for k numbers chosen out of 1:n Roger Stafford
From: helper on 7 May 2008 00:13 > I believe the call > > c = nchoosek(1:n,k); > > does just what you want for k numbers chosen out of 1:n > > Roger Stafford > NCHOOSEK only gives combinations *without* replacement, so he won't get the 1 1 2 2 3 3 elements.
From: Daniel on 7 May 2008 00:44 > You are looking for all combinations with replacement. > Check out the file COMBSREP.m at the following link: > > http://www.mathworks.com/support/solutions/files/s36265/ > > combsrep(1:3, 2) 'combsrep' will work if the sets I'm comparing are all the same, but it lacks the functionality of 'allcomb', which allows the sets to contain different elements. So to give another example: >> allcomb([1:3], [1:4]) ans = 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 But I need: ans = 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 I don't think I can do this with combsrep (or am I missing something)? Thanks for the link though (and thanks to anyone else who's replied), combsrep looks like a useful function anway.
|
Next
|
Last
Pages: 1 2 3 Prev: Displaying sub panels in GUI Next: Integration inside loop does not work |