Generate matrix of bits

后端 未结 3 499
别跟我提以往
别跟我提以往 2021-01-21 04:52

I would like to take an integer n defining the number of bits in my communication code and a vector defining the alphabet I am assigning to bits 0:n-1,

3条回答
  •  臣服心动
    2021-01-21 05:10

    dec2bin is actually not the best way to approach the problem, because its result is an array of strings (i.e a matrix of characters), where each digit is a character. If you want '-1' to represent a logical "0", that would be two characters, and it introduces problems.

    Consider an alternative method with bitget. Making use of Shai's suggestion, do the following:

    [bits, values] = meshgrid(1:3, 0:7);
    M = 2 * bitget(values, bits) - 1;
    

    The will produce what you want:

    M =
        -1    -1    -1
         1    -1    -1
        -1     1    -1
         1     1    -1
        -1    -1     1
         1    -1     1
        -1     1     1
         1     1     1
    

提交回复
热议问题