Rounding decimals matlab manipulates

2 views (last 30 days)
yuval ohayon
yuval ohayon on 31 Jul 2017
Commented: Image Analyst on 2 Aug 2017
I need to write, in 1 code line, given a number (pos, neg, int, float), how do I round it without preset functions like fix, round, etc.?
  9 Comments
Steven Lord
Steven Lord on 2 Aug 2017
In general people on MATLAB Answers don't post answers to homework questions or questions that sound like homework. If you show what you've done to try to solve the problem on your own and ask a specific question about where you're having difficulty you may receive some more specific suggestions.
Of course, if this isn't a homework problem, just use the round function or the other rounding functions included in MATLAB.

Sign in to comment.

Answers (1)

Jan
Jan on 1 Aug 2017
Try it:
x = 13.3
y = mod(x, 1)
z = 13.6
y = mod(x, 1)
Now you should see how to use the output to decide, if the rest of the division by 1 is lower or higher than 0.5.
  3 Comments
Jan
Jan on 2 Aug 2017
int64() rounds also. And you can use cast() to preserve the original class.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!