stopping matlab from re-formating my numbers!!!

5 ビュー (過去 30 日間)
Nicholas
Nicholas 2013 年 9 月 7 日
>> x = 110.01001
x =
110.0100
>> z = num2str(x)
z =
110.01
>> w = z-'0'
w =
1 1 0 -2 0 1
This is what happens when I try convert my input value "x" to a vector "w". Matlab deletes the last 3 numbers from my binary number 110.01001. I am trying to create a function that converts from binary to decimal and this obviously screws everything up. How can I stop matlab from doing cutting off my numbers?????????
  1. so aggravating
  1 件のコメント
Jan
Jan 2013 年 9 月 8 日
Does "w = z-'0" explain any useful detail here? If so, which one?

サインインしてコメントする。

回答 (3 件)

Jan
Jan 2013 年 9 月 8 日
There is no general method, which converts between binary and decimal numbers 100% accurately. Many decimal numbers do not have an exact representation in binary format with a limited number of bits, and the other way around. A very common example is:
0.3 - 0.2 - 0.1
This does not produce 0.0 due to the limited precision.
So your problem, that the default format of num2str cuts off the digits you want to include, cannot be solved by choosing another format. You have to decide, if you want to treat 2.999999999999999 as 3.0 or not.

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 7 日
x = 110.01001
a=sprintf('%.5f',x)
  2 件のコメント
Nicholas
Nicholas 2013 年 9 月 7 日
I need this to work for any binary number, I believe this will work for only this example right??
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 7 日
If you do not specify the number of decimals, you can not get it work. Because 1.000 % or 1.00000 are equal to 1

サインインしてコメントする。


Jan
Jan 2013 年 9 月 7 日
Use the format argument when calling num2str
e.g.
z = num2str( x, '%4.10f' )
if you want 10 (ten, not three ;) ) decimals. Look into num2str's helpfile for more information on number formatting.

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by