필터 지우기
필터 지우기

change number of sig figs in a list of numbers

조회 수: 2 (최근 30일)
Michael
Michael 2011년 5월 24일
basically i have a list of numbers, all with the same number of significant figures. an example of a number is :
734504.0749692708
I want to make this number the following: 734504.07
But the important thing is that I do not want to round. Also, I want to actually change the value, not simply how it is displayed. Matlab knows the exact value, and only displays 5 sig figs from the total of 16. I want to completely drop the last 8 figures in order to give me the above number. any way to do this?

채택된 답변

Matt Fig
Matt Fig 2011년 5월 24일
N = 734504.0749692708
Ns = round(N*100)/100
EDIT Addressing Oleg's concern...
The above keeps two significant decimals. If you instead want to just drop them, then use fix instead. Look at the difference:
N = 7.85999999
Ns1 = round(N*100)/100
Ns2 = fix(N*100)/100
N =
7.85999999
Ns1 =
7.86
Ns2 =
7.85
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 5월 24일
You should change it to fix otherways the following number is rounded:
N = 734504.079;
Matt Fig
Matt Fig 2011년 5월 24일
I would think that 734504.08 would be the correct solution in keeping 2 significant decimals?
If it is desired to just drop any digits after the second decimal, then you are correct...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2011년 5월 24일
more (EDIT)
N = 734504.0749692708
Ns = fix(N*100)/100
more more
"I want to completely drop the last 8 figures in order to give me the above number."(Michael):
pwr = 10^(8-ceil(log10(N)))
Ns = fix(N*pwr)/pwr
  댓글 수: 1
Matt Fig
Matt Fig 2011년 5월 24일
ROUNDN is only available with the mapping toolbox, correct?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 5월 24일
It is not possible to store exactly 734504.07 in MATLAB double .
The algorithms that Matt and Andrei posted can only produce approximations of the number you want. For example, Andrei's solution produces the value 734504.069999999948777258396148681640625 . This is not a bug in Andrei's solution, but rather a limitation in what can be represented in MATLAB without using the Fixed Point Toolbox or the Symbolic Math Toolbox.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by