问题
From the vector V=[3 31 40], how to find the following combination:
3 31 40
3 31 140
3 131 40
3 131 140
103 31 40
103 31 140
103 131 40
103 131 140
the construction is done by following the logic of the combination of 0 and 1, but by adding 100
回答1:
Using this:
V=[3 31 40] ;
comb = dec2bin((0:2^numel(V)-1).') ; %'// generate all the possible binary combinations
cl = logical( double(comb)-48 ) ; %// translate them to an array of logical
Vout = repmat( V , size(cl,1),1 ) + cl.*100 ; %// replicate the initial array and add `100` when relevant
Will give you:
Vout =
3 31 40
3 31 140
3 131 40
3 131 140
103 31 40
103 31 140
103 131 40
103 131 140
You can compact it in one line if you want:
Vout = repmat( V , size(cl,1),1 ) + (double(dec2bin((0:2^numel(V)-1).'))-48).*100 ;
来源:https://stackoverflow.com/questions/27825298/matlab-how-to-build-a-combination-according-to-the-logical-0-and-1-but-by-addin