Dec to bin: Difference between revisions

From QETLAB
Jump to navigation Jump to search
Created page with "{{Function |name=dec_to_bin |desc=Converts a decimal number to a binary number in a similar way to the dec2bin function in MATLAB. |cat=List of functions#Helper_functions|He..."
 
No edit summary
Line 6: Line 6:
|v=0.50
|v=0.50
|helper=1}}
|helper=1}}
<tt>'''dec_to_bin'''</tt> is a [[List of functions|function]] that computes the identity matrix of the desired size, which is either full or sparse.
<tt>'''dec_to_bin'''</tt> is a [[List of functions|function]] that takes a decimal number as input and converts it to a vector representing the binary form of the number with the MSB to the right.  


==Syntax==
==Syntax==
* <tt>ID = iden(DIM,SP)</tt>
* <tt>BIN = dec_to_bin(DEC)</tt>


==Argument descriptions==
==Argument descriptions==
* <tt>DIM</tt>: The number of rows (or equivalently, columns) that <tt>ID</tt> will have.
* <tt>DEC</tt>: The decimal number to be converted to a vector representing the binary form of <tt>DEC</tt>. This must be a non-negative integer.  
* <tt>SP</tt>: A flag (either 1 or 0) indicating whether <tt>ID</tt> should or should not be sparse.
 
==Examples==
==Examples==
Full and sparse versions of the 2-by-2 identity matrix:
Some basic examples of how dec_to_bin works.
<syntaxhighlight>
<syntaxhighlight>
>> iden(2,0)
>> dec_to_bin(5)


ans =
ans =


     1    0
     1    0    1
    0    1


>> iden(2,1)
>> dec_to_bin(6)


ans =
ans =


  (1,1)        1
    0    1     1
  (2,2)        1
</syntaxhighlight>
</syntaxhighlight>


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


{{SourceCode|name=iden|helper=1}}
{{SourceCode|name=dec_to_bin|helper=1}}

Revision as of 11:37, 18 July 2018

dec_to_bin
Converts a decimal number to a binary number in a similar way to the dec2bin 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 decimal 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 decimal 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 here to view this function's source code on github.