Approximate the variable in the workspace to one digite ?

1 回表示 (過去 30 日間)
Mary Jon
Mary Jon 2014 年 11 月 19 日
編集済み: David Young 2014 年 11 月 19 日
Hi all
I am have 1000 variables in the workspace, each one have five digits after the comma like 50,44444
How aproximated all 1000 variables to one digit after the comma like 50,4 ? because I cant chang each one individually ? so difficult!

採用された回答

David Young
David Young 2014 年 11 月 19 日
編集済み: David Young 2014 年 11 月 19 日
If you mean you have 1000 values stored in an array, and you want to change the stored values to be rounded to one decimal place, then you can do this:
Xround = round(X*10)/10;
[Edit: please note that if you have R2014b or later, there is a neater solution
Xround = round(X, 1);
as pointed out by ImageAnalyst.]
If you mean you want to print the values showing only one decimal place, then you can do this:
fprintf('%8.1f\n', X);
varying the format string as necessary to get the output you want.
If you mean you have 1000 different variables, perhaps with names like x0001, x0002 etc., then you should change the structure of your program, and store the values in an array.

その他の回答 (2 件)

Star Strider
Star Strider 2014 年 11 月 19 日
See the documentation for format. You can set the numeric display to one of several options. (The data themselves are all stored to the same precision, so the display precision does not affect the data precision.)

Image Analyst
Image Analyst 2014 年 11 月 19 日
Use the round function:
m=rand(4) % Tons of digits.
m1 = round(m, 1) % Round to first digit.
Hopefully your thousands of variables are in an array and not truly thousands of separate variables, which would be bad programming practice.
  1 件のコメント
David Young
David Young 2014 年 11 月 19 日
Well I learnt something from that! I hadn't noticed the new optional arguments for round()!

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by