Matlab:如何根据逻辑0和1但加100来构建组合? [英] Matlab: how to build a combination according to the logical 0 and 1 but by adding 100?

查看:87
本文介绍了Matlab:如何根据逻辑0和1但加100来构建组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从向量V=[3 31 40]中查找以下组合:

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

通过遵循01组合的逻辑来完成构造,但是要添加100

the construction is done by following the logic of the combination of 0 and 1, but by adding 100

推荐答案

使用此方法:

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

会给您:

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  ;

这篇关于Matlab:如何根据逻辑0和1但加100来构建组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆