필터 지우기
필터 지우기

Truncating fractions

조회 수: 5 (최근 30일)
Raviteja
Raviteja 2011년 4월 7일
If A I have like this
>> A=[0.0001 -0.0012 1.0005 0.0040 1.4125]
A =
0.0001 -0.0012 1.0005 0.0040 1.4125
I want to have A values like this
0.00 0.00 1.00 0.00 1.41
How to do in MATLAB ?

채택된 답변

Matt Fig
Matt Fig 2011년 4월 7일
Ar = round(A*100)/100
.
.
.
EDIT
Raviteja, the above command does store your values as you want. The problem is not with the values, but with how the values are displayed in MATLAB. You cannot make MATLAB display the values in just any way you want. The closest you can get is:
format bank
Ar = round(A*100)/100
Ab = abs(A) %This looks like what you want, but the full value is there.
  댓글 수: 2
Raviteja
Raviteja 2011년 4월 7일
>> Ar=round(A*100)/100
Ar =
0 0 1.0000 0 1.4100
Matt Fig
Matt Fig 2011년 4월 7일
It looks like it is working to me. Did you mean that you want to print out the array? That is a different question.
sprintf('%3.2f ',abs(A.'))

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

추가 답변 (2개)

Oleg Komarov
Oleg Komarov 2011년 4월 7일
Matt's solution is working. Your question isn't specific enough. You want to display, and not to truncate.
A=[0.0001 -0.0012 1.0005 0.0040 1.4125]
sprintf('%4.2f ',A)
Oleg
  댓글 수: 2
Raviteja
Raviteja 2011년 4월 7일
I dont need display, I want to make values only like this
0.00 0.00 1.00 0.00 1.41
more over if I want to store
Ar=sprintf('%4.2f ',A);
It making Ar as char.
This is not what I want.
I need "double" values which exactly comes like
A=
0.00 0.00 1.00 0.00 1.41
Walter Roberson
Walter Roberson 2011년 4월 7일
Raviteja, it is impossible for binary value representation in *any* programming language to store 1.41 _exactly_.

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


Walter Roberson
Walter Roberson 2011년 4월 7일
What you would like to do cannot be done in any finite binary number representation system. 1/10 is an infinitely repeating number in binary, just the same way that 1/7 is an infinitely repeating number in decimal.
You can represent A to two decimal places as character strings for display purposes, but you will not be able to truncate to two decimal places numerically in binary.
>> sprintf('%.53g', 1.41)
ans =
1.4099999999999999200639422269887290894985198974609375

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by