フィルターのクリア

generate random numbers in engineering notation with precision

3 ビュー (過去 30 日間)
Mike Sirwood
Mike Sirwood 2013 年 1 月 11 日
Matlab folks,
I have a simple question: I want to generate a vector of random numbers formatted in engineering format (e-notation) and the precision is x.xxxxExx
The precision is what is causing me trouble especially is that my goal is to generate a vector of these
Thanks

採用された回答

José-Luis
José-Luis 2013 年 1 月 11 日
編集済み: José-Luis 2013 年 1 月 11 日
By default, variables in Matlab are double. If you want to change the way they are displayed, please look at:
doc format
If you want to save them to a text file, please look at:
doc fprintf
  4 件のコメント
Mike Sirwood
Mike Sirwood 2013 年 1 月 11 日
Ok, let me make it simpler, if I used fprintf as you suggested, then how do I store its "formatted output" in a variable as a number?
José-Luis
José-Luis 2013 年 1 月 11 日
編集済み: José-Luis 2013 年 1 月 12 日
You can't. Not as an "engineering" number. Not unless you create a custom data type. And Matlab would definitely not be the tool I would choose for that job. If I understand correctly your intentions, then you might be better off creating a vector of integer values.
precision_you_want = 1e-8; %for instance
your_interval = 1e-8:precision_you_want:1e-4;
numRand = 1000;
your_vector = your_interval(randi(numel(your_interval),1,numRand));
But I still think it is better to round of to the precision you want. There is no "engineering" data type, and mimicking one would be difficult, especially since most computers use binary, not decimal, format.

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

その他の回答 (2 件)

Rick Rosson
Rick Rosson 2013 年 1 月 11 日
  1 件のコメント
Mike Sirwood
Mike Sirwood 2013 年 1 月 11 日
But when I use this function, for example and using a number like
num2eng(0.00054089)
ans = 540.89e-6
I want it in the format x.xxxxEx and not as a string

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


Daniel Shub
Daniel Shub 2013 年 1 月 11 日
What do you mean by format? Computers tend to work with integers and floats, with floats coming in single and double precision varieties. They are internally represented as a sequence of 0's and 1's and many programs can externally display them in a variety of formats (binary, hex, decimal, engineering, etc). MATLAB by default uses double precision floats, but you can switch to single precision or integers. The format in which the number is displayed is controlled by the FORMAT function, but this has no effect on the internal representation.

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by