本文介绍了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来构建组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:48