Main Content

num2bin

Convert number to binary representation using quantizer object

Description

y = num2bin(q,x) converts the numeric array x into a binary character vector returned in y using the data type properties specified by the quantizer object q.

If x is a cell array containing numeric matrices, then y will be a cell array of the same dimension containing binary strings. If x is a structure, then each numeric field of x is converted to binary.

[y1,y2,…] = num2bin(q,x1,x2,…) converts the numeric matrices x1, x2, … to binary strings y1, y2, ….

example

Examples

collapse all

Convert a matrix of numeric values to a binary character vector using the attributes specified by a quantizer object.

x = magic(3)/9
x = 3×3

    0.8889    0.1111    0.6667
    0.3333    0.5556    0.7778
    0.4444    1.0000    0.2222

q = quantizer([5,3])
q =


        DataMode = fixed
       RoundMode = floor
    OverflowMode = saturate
          Format = [5  3]
y = num2bin(q,x)
y = 9x5 char array
    '00111'
    '00010'
    '00011'
    '00000'
    '00100'
    '01000'
    '00101'
    '00110'
    '00001'

Convert between a binary character vector and a numeric array using the properties specified in a quantizer object.

Convert Numeric Array to Binary String

Create a quantizer object specifying a word length of 4 bits and a fraction length of 3 bits. The other properties of the quantizer object take the default values of specifying a signed, fixed-point data type, rounding towards negative infinity, and saturate on overflow.

q = quantizer([4 3])
q =


        DataMode = fixed
       RoundMode = floor
    OverflowMode = saturate
          Format = [4  3]

Create an array of numeric values.

[a,b] = range(q);
x = (b:-eps(q):a)
x = 1×16

    0.8750    0.7500    0.6250    0.5000    0.3750    0.2500    0.1250         0   -0.1250   -0.2500   -0.3750   -0.5000   -0.6250   -0.7500   -0.8750   -1.0000

Convert the numeric vector x to binary representation using the properties specified by the quantizer object q. Note that num2bin always returns the binary representations in a column.

b = num2bin(q,x)
b = 16x4 char array
    '0111'
    '0110'
    '0101'
    '0100'
    '0011'
    '0010'
    '0001'
    '0000'
    '1111'
    '1110'
    '1101'
    '1100'
    '1011'
    '1010'
    '1001'
    '1000'

Use bin2num to perform the inverse operation.

y = bin2num(q,b)
y = 16×1

    0.8750
    0.7500
    0.6250
    0.5000
    0.3750
    0.2500
    0.1250
         0
   -0.1250
   -0.2500
      ⋮

Convert Binary String to Numeric Array

All of the 3-bit fixed-point two's-complement numbers in fractional form are given by:

q = quantizer([3 2]);
b = ['011  111'
     '010  110'
     '001  101'
     '000  100'];

Use bin2num to view the numeric equivalents of these values.

x = bin2num(q,b)
x = 4×2

    0.7500   -0.2500
    0.5000   -0.5000
    0.2500   -0.7500
         0   -1.0000

Input Arguments

collapse all

Data type properties to use for conversion, specified as a quantizer object.

Example: q = quantizer([16 15]);

Numeric input array, specified as a scalar, vector, matrix, multidimensional array, cell array, or structure.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | struct | cell

Tips

  • num2bin and bin2num are inverses of one another. Note that num2bin always returns the binary representations in a column.

Algorithms

  • The fixed-point binary representation is two's complement.

  • The floating-point binary representation is in IEEE® Standard 754 style.

Version History

Introduced before R2006a