0

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

bzak
  • 563
  • 1
  • 8
  • 21
  • 2
    Use [this](http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors) with `vectors = { [3 103], [31 131], [40 140] };`. Let me know if that fully serves your needs, so that I can close as duplicate – Luis Mendo Jan 07 '15 at 17:45
  • 1
    Or, since this is the binary case, you can use the simpler `combs = bsxfun(@plus, 100*(dec2bin(0:2^numel(V)-1)-'0'), V)` – Luis Mendo Jan 07 '15 at 17:47

1 Answers1

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  ;
Hoki
  • 11,637
  • 1
  • 24
  • 43