Dec to bin

From QETLAB
Jump to: navigation, search
dec_to_bin
Converts a number to its binary form in a similar way to the de2bi function in MATLAB.

Other toolboxes required none
Function category Helper functions
This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

dec_to_bin is a function that takes a number as input and converts it to a vector representing the binary form of the number with the MSB to the right.

Syntax

  • BIN = dec_to_bin(DEC)

Argument descriptions

  • DEC: The number to be converted to a vector representing the binary form of DEC. This must be a non-negative integer.

Examples

Some basic examples of how dec_to_bin works.

>> dec_to_bin(5)
 
ans =
 
     1     0     1
 
>> dec_to_bin(6)
 
ans =
 
     0     1     1

Notes

This function is only meant to simplify code within other functions without the need to install the communications package of MATLAB or Octave-Forge.

Source code

Click on "expand" to the right to view the MATLAB source code for this function.

  1. %%  IDEN  Computes a sparse or full identity matrix
  2. %   This function has two required arguments:
  3. %     DIM: the number of rows (or columns) of the identity matrix
  4. %     SP: a flag (1 or 0)
  5. %
  6. %   ID = iden(DIM,SP) returns the DIM-by-DIM identity matrix. If SP = 0
  7. %   then ID will be full. If SP = 1 then ID will be sparse.
  8. %
  9. %   Only use this function within other functions to easily get the correct
  10. %   identity matrix. If you always want either the full or the sparse
  11. %   identity matrix, just use MATLAB's built-in eye or speye function.
  12. %
  13. %   URL: http://www.qetlab.com/iden
  14.  
  15. %   requires: nothing
  16. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
  17. %   package: QETLAB
  18. %   last updated: November 28, 2012
  19.  
  20. function id = iden(dim,sp)
  21.  
  22. if(sp)
  23.     id = speye(dim);
  24. else
  25.     id = eye(dim);
  26. end