Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing Bit modification on Floating point numbers in Matlab

I'm working in Matlab using Non-negative Matrix factorization to decompose a matrix into two factors. Using this I get from A two double precision floating point matrices, B and C.

sample results are

B(1,1) = 0.118
C(1,1) = 112.035

I am now trying to modify specific bits within these values but using the bitset function on either values I get an error because bitset requires unsigned integers.

I have also tried using dec2bin function, which I assumed would convert decimals to binary but it returns '0' for B(1,1).

Does anyone know of any way to deal with floats at bit level without losing precision?

like image 318
Michael Allen Avatar asked Mar 09 '11 12:03

Michael Allen


1 Answers

You should look into the typecast and bitset functions. (Doc here and here respectively). That lets you do stuff like

xb = typecast( 1.0, 'uint64' );
xb = bitset( xb, 10, 1 );
typecast( xb, 'double' );
like image 134
Edric Avatar answered Nov 10 '22 20:11

Edric