how to set rounded to 4 decimal places for a vector?

17 ビュー (過去 30 日間)
Stanley Cheng
Stanley Cheng 2014 年 1 月 20 日
コメント済み: José-Luis 2014 年 1 月 20 日
Hi everyone,
I have a 12*1 vector and all the 12 elements are numerical values. But they have too many decimal places, but 4 decimal places are enough for me. So how can I set all the numerical values in the vector to 4 decimal places ? Furthermore, I have 10 this kind of vector.
Thanks so much!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日
編集済み: Azzi Abdelmalek 2014 年 1 月 20 日
This will round to 4 decimals
u=rand(1,10)
n=4
y=round(u*10^n)/10^n
Now if you want to display for decimals, without rounding
sprintf('%10.4f',u')

その他の回答 (1 件)

José-Luis
José-Luis 2014 年 1 月 20 日
You should realize that even if you display them with four decimal places, they would still be stored as doubles in memory, i.e. the only thing you would be achieving is a loss of precision.
You could display them in a different format.
a = rand(10,1)
format bank
a
Please look at the documentation for format
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日
When we don't need the fifth decimal, it's not a loss of precision
José-Luis
José-Luis 2014 年 1 月 20 日
Calculate with as much precision as possible, display only what makes sense is a good rule of thumb to go by.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by