Stop exponential answers
515 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
I have two values representing the max and min of a data vector.
max_val = 0.9855;
min_val = 0.9851;
Both are of type 'double'. However, when I try to subtract the min from the max:
diff = max_val - min_val;
the answer 3.6023e-04 is returned instead of 0.0004. How can I ensure that this answer is expressed without the exponentiation?
1 件のコメント
Vishal Singh
2021 年 2 月 26 日
you can use "format short g" command in the start of the code.I am a fresher in matlab but as far as i know it can help to get rid of e in the answer.
採用された回答
Titus Edelhofer
2011 年 6 月 7 日
Hi,
there is no type in format that generally tells MATLAB to use such a format. Nevertheless you can always use sprintf to make a string in the format you like, in this case it would be
sprintf('%f', diff)
ans =
0.000400
Titus
3 件のコメント
Guillaume
2016 年 8 月 11 日
format does not alter the result either. It only alters the way it is displayed.
And of course, it is much simpler to change the format to short/long g and display the number as usual than having to call sprintf all the time.
John D'Errico
2016 年 8 月 11 日
I wish we could upvote comments. This one by Guillaume is correct of course. (The answer by Titus is also correct.) But the point is, people seem to think that use of the format command actually changes the number internally. That does not happen. Format only affects the output as it appears in the command window.
その他の回答 (2 件)
John D'Errico
2011 年 6 月 7 日
I tend to work normally with the display format set as
format short g
for most work. This gives me numbers in a format that I like as often as possible, only going into scientific notation when necessary. Thus
>> format short g
>> X = 3.6023e-04
X =
0.00036023
"short g" is more compact than the alternative of "long g", and most of the time I don't need to see 15 decimal digits in my results.
0 件のコメント
Robert Cumming
2011 年 6 月 7 日
to make it write the number you could do:
format long g
to actually round your number to the correct number of decimal places you could make an inline function, e.g.:
dcp = inline ( 'round(input.*10.^number)./10.^number' )
dcp(diff,4) % this will round to 4 decimal places
p.s. its not an exponential its engineering format that your answer is expressed as.
2 件のコメント
John D'Errico
2011 年 6 月 7 日
Note that inline functions are not at all efficient. function handles are far better choices.
dcp = @(inp,number) round(inp.*10.^number)./10.^number;
It is also a poor choice to define a variable with the name input, as this overloads the existing matlab function input.m, preventing you from later using that tool.
Robert Cumming
2011 年 6 月 7 日
it was just a simple example to show what could be done. My actual preference is to have a seperate function which is saved on my path so its always available to me and my team.
As far as using input - I get your point but in simple small functions I dont think its an issue.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!