matlab - How can I create all combinations of characters in sets of text? -
for example, have sets of text so:
column 1:
a b column 2:
l m n column 3 :
v w x y and want combine them output this:
alv alw alx aly amv amw amx amy ... which output 24 combinations of text. if use first 2 columns, output 2*3=6 combinations.
i'm not able figure out how in matlab. suggestions?
one solution use function ndgrid generate of combinations of indices sets:
c = {'ab' 'lmn' 'vwxy'}; %# cell array of text sets sizevec = cellfun('prodofsize',c); %# vector of set sizes [index3,index2,index1] = ndgrid(1:sizevec(3),... %# create index 1:sizevec(2),... %# combinations 1:sizevec(1)); %# sets combmat = [c{1}(index1(:)); ... %# index each corresponding cell of c , c{2}(index2(:)); ... %# concatenate results 1 matrix c{3}(index3(:))].'; and should following combmat:
alv alw alx aly amv amw amx amy anv anw anx blv blw blx bly bmv bmw bmx bmy bnv bnw bnx bny if want combinations columns 1 , 2, remove first input , output arguments call ndgrid , remove c{3}(index3(:)) computation of combmat.
if instead want c cell array of cell arrays of strings instead of cell array of character arrays, can still use exact same code above. difference combmat end being cell array of strings instead of character array.
update:
i've created generalized solution can compute combinations number of sets (either character arrays or cell arrays of strings). can find in this answer closely-related question. reproduce above example, call so:
combmat = allcombs(c{:});
Comments
Post a Comment