Hi all!
I have two matrices. They have one column. First matrix is:
7.551119
7.551154
7.551189
7.551224
Second matrix is:
7.551111
7.551146
7.551181
7.551215
I want to comparison two columns but only 5 decimal places. I don't like round my numbers like:
7.55112
7.55115
and so on, if I use the function round.

댓글 수: 2

Walter Roberson
Walter Roberson 2015년 12월 1일
When you say that you want to compare to 5 decimal places, do you mean that when printed out rounded to 5 decimal places the numbers must match, or do you mean that the difference between the two numbers must be less than 10^(-5) ? Or at most 1/2 * 10^(-5) ?
Thar
Thar 2015년 12월 2일
I want to keep the 5 decimal points, but not rounded the numbers. for example for the number 7.551187 i want to keep only the 7.55118 and not round to 7.55119.

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

 채택된 답변

arich82
arich82 2015년 12월 2일

1 개 추천

You can truncate (without rounding) using floor (or fix, if your numbers can be negative):
x = [ ...
7.551119; ...
7.551154; ...
7.551189; ...
7.551224; ...
];
y = fix(x*1e5)/1e5;
output:
y =
7.551110000000000
7.551150000000000
7.551180000000000
7.551220000000000

추가 답변 (2개)

Thorsten
Thorsten 2015년 12월 2일
편집: Thorsten 2015년 12월 2일

0 개 추천

You can round to 5 decimal places using
xr = round(x*1e5)/1e5;
You may want to set
format long
such that the result is properly displayed.

댓글 수: 5

Or simply
xr = round(x, 5);
unless one is using an antique version of MATLAB.
Walter Roberson
Walter Roberson 2015년 12월 2일
1.234563 rounds to 1.23456
1.234565 rounds to 1.23457
but the numbers differ by only 0.000002
This is why it is necessary to know whether the question is about whether the values are equal when rounded or if the question is whether the values differ by less than a threshold.
Thar
Thar 2015년 12월 2일
The question is whether the values are equal, but i don't like to round the values. for example if i have the values 7.551119 and 7.551111 i want to keep for each value 5 decimal points, 7.55111 and 7.55111.These values are equal and this is i want. If i round the values the first value will be 7.55112 and the second is 7.55111.I don't like this.
Image Analyst
Image Analyst 2015년 12월 2일
Then don't round (as I suspected you shouldn't). Use the tolerance like I told you in my answer where I referred you to the FAQ.
Walter Roberson
Walter Roberson 2015년 12월 2일
To check, Thodoris, you are okay with 7.551111 comparing equal to 7.551119, but 7.551119 should not compare equal to 7.551121 even though that is closer to it than 7.551111 is?

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2015년 12월 1일

댓글:

2015년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by