rounding elements in matrix if > or < to 0.75

example:
3.32==> .032 is < 0.75 ===> 3
0.78===>0.78 is >0.75==> 1

1 Comment

What should be the result when the decimal part is equal to 0.75?

Sign in to comment.

Answers (2)

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 Comments

" is still rounded up. 1.74 becomes 1.54"
do you mean "is still rounded up. 1.74 becomes (1.74-.25) 1,49"
thanks
aldo
aldo on 17 Jul 2023
Moved: Cris LaPierre on 17 Jul 2023
fixed the code you wrote But I wrote wrong I want to put a decimal input and then the code runs: Ex: .75.. .82 ... .25 and if <x I round down otherwise up
do you think this code is correct?
function ris=Test_round(k)
x = [3.32;0.78;1.25;0.20];
ris=round(x-(k-0.5));
end
matrix K
input:dec
x=0.75
x=.25
x=.91
You can determine if it works just by testing it. Do you get the expected results?
ah ok...I'll have to do more appropriate numerical tests I'm bad at math :(

Sign in to comment.

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!

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 17 Jul 2023

Commented:

on 18 Jul 2023

Community Treasure Hunt

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

Start Hunting!