rounding elements in matrix if > or < to 0.75

조회 수: 4 (최근 30일)
aldo
aldo 2023년 7월 17일
댓글: Dyuman Joshi 2023년 7월 18일
example:
3.32==> .032 is < 0.75 ===> 3
0.78===>0.78 is >0.75==> 1
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 7월 18일
What should be the result when the decimal part is equal to 0.75?

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

답변 (2개)

Cris LaPierre
Cris LaPierre 2023년 7월 17일
편집: Cris LaPierre 2023년 7월 17일
Currently, round uses 0.5 as the threshold. The quickest solution would be to modify your data by subtracting 0.25 to artificially move the threshold. Now, 0.75 become 0.5 and is still rounded up. 1.74 becomes 1.49, and is rounded down. In both cases, you end up with the value you want.
x = [3.32;0.78];
round(x-0.25)
ans = 2×1
3 1
  댓글 수: 5
Cris LaPierre
Cris LaPierre 2023년 7월 17일
You can determine if it works just by testing it. Do you get the expected results?
aldo
aldo 2023년 7월 17일
ah ok...I'll have to do more appropriate numerical tests I'm bad at math :(

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


Swapnil Tatiya
Swapnil Tatiya 2023년 7월 18일
Hi @aldo,
you could use this as another way to round your numbers according to what you desire by doing the following
x=[0.33 0.45 1.56 1.77];
x=floor(x+0.25)
x = 1×4
0 0 1 2
Hope this helps!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by