Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating vectors in MATLAB

Tags:

vector

matlab

I am wondering whether there is an efficient way in MATLAB to generate all vectors of a fixed length with elements from a finite set.

For instance, how can I build all vectors of length 5 with only 0 or 1 as elements?

like image 381
Kiran Mudassir Avatar asked Feb 26 '10 18:02

Kiran Mudassir


4 Answers

The proper way to build all vectors of length 5 with only 0 or 1 as elements is

a = dec2bin(0:31,5) - '0';

I hope you see why.

like image 150
AVB Avatar answered Nov 15 '22 10:11

AVB


not quite what you need, but permv generates vector permutations. If you do not find exact solution, you can adapt vector permutations.

permv

like image 41
Anycorn Avatar answered Nov 15 '22 09:11

Anycorn


MathWorks' FileExchange is your friend:

  • VChooseKRO
  • COMBN
  • COMBINATOR
  • NPERMUTEK

So using the first function, its as easy as:

VChooseKRO([0 1], 5)

You can even make it work for a cell array of strings as the finite set:

C = {'a' 'b' 'c' 'd'};
C( VChooseKRO(1:numel(C), 2) )
like image 35
merv Avatar answered Nov 15 '22 09:11

merv


New answer:

You can speed up the DEC2BIN solution suggested by AB (with an assist by woodchips) by instead using the BITGET function. There are two ways to approach this. The first uses the REPMAT function to make the inputs equal in size (both 32-by-5 matrices):

allCombos = bitget(repmat((0:31)',1,5),repmat(5:-1:1,32,1));

And the second simply calls BITGET once for every individual bit you want to get:

vec = (0:31)';
allCombos = [bitget(vec,5) bitget(vec,4) bitget(vec,3) ...
             bitget(vec,2) bitget(vec,1)];

And here are some sample timings:

     Method      |  Average Time
-----------------+------------------
  DEC2BIN        |   0.000788 s
  BITGET+REPMAT  |   0.000727 s
  BITGET x5      |   0.000045 s

As you can see, the unrolled BITGET is very fast.


Old answer: (for posterity)

If you are wanting to build a matrix of every possible vector of zeroes and ones that has a length of 5, this would be one way to do it using the functions PERMS and UNIQUE (since PERMS creates repeated rows):

allCombos = [0 0 0 0 0;
             unique(perms([0 0 0 0 1]),'rows'); ...
             unique(perms([0 0 0 1 1]),'rows'); ...
             unique(perms([0 0 1 1 1]),'rows'); ...
             unique(perms([0 1 1 1 1]),'rows'); ...
             1 1 1 1 1];
like image 37
gnovice Avatar answered Nov 15 '22 09:11

gnovice