Matlab: how to build a combination according to the logical 0 and 1 but by adding 100?

左心房为你撑大大i 提交于 2019-12-12 03:23:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!