Is there anyway to convert a number to binary without using dec2bin?

100 views (last 30 days)
Dear everyone
I want to convert these values to binary without using dec2bin
why I do not want dec2bin because for each value the result will be 8 bits this maximize the numbers of values and I want these number 21 values no more no less
in the same way that im2bw work I did it but the result are all ones
Here are the values 77.152,128.35,136.04,191.57,222.5,241.67,254.83,272.7,251.13,286.17,310.83,305.48,303.8,301.91,318.85,365.8,350.72,301.91,220.91,145.37,94.304
Here are the values after applying dec2bin(The problem: the number of values is very huge and I want it 21 values like the Original values) 0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,1,1,1,0,1,0,0,0,0,0,0,1,1,0,1,0,1,0,0,0,1,0,1,1,0,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1,1,1,1,0,1,1,1,0,1,0,1,1,0,0,1,1,1,1,1,1,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,1,0,1,0,1,0,1,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0
Here are the values after applying im2bw(The problem:all the values are ones and I do not want that )
Please,give me answers?
Eric on 6 Apr 2015
I haven't the slightest idea what you're trying to accomplish, but all im2bw does is threshold an image. If you're not supplying the second argument (level), the default value of 0.5 is used. For an input image that is floating point then all pixel values greater than 0.5 will be 1. That's why im2bw is giving you all ones. The output of im2bw is a logical array and clearly not appropriate for whatever it is you're trying to do.

Sign in to comment.

Answers (2)

Stephen23 on 6 Apr 2015
Edited: Stephen23 on 7 Apr 2015
This is an ill-defined problem, unless you specify what encoding method you want to convert decimal fractions. MATLAB only has dec2bin which converts integer values only. This is a topic that has been discussed before on MATLAB Answers, and even some other forums:
Although it is important to note that the problem is ill-defined, because "There is no one right answer on how to represent a fractional number in binary" (quoted from Peter at the last link).

Mahdiyar on 6 Apr 2015
Hi Ameligege
It is simple . you can use the function de2bi instead of dec2bi.
  1 Comment
Ameligege on 6 Apr 2015
An error occur when I test it on single value
Error using de2bi (line 127) Input must contain only finite real nonnegative integers.

Sign in to comment.


Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by