3

Is there a (Matlab) function similar to ceil to find the next integer that is greater than the input but not equal to the input?

Examples:

1.1 --> 2
1.9 --> 2
2.0 --> 3    (note that ceil(2) == 2)
2.1 --> 3

I tried with ceil(x+eps), but that only works for small numbers:

>> ceil((-4:4)+eps)

ans =

-4    -3    -1     0     1     2     2     3     4

Also, any equivalent for floor?

Laurenz
  • 1,810
  • 12
  • 25

2 Answers2

5

If you want to treat negative and positive values alike, such that 1.1 becomes 2 and -1.1 becomes 1, then the answer from informaton using floor is correct:

out = floor(x)+1;

If you'd rather have positive values become the next-most positive integer, but negative values become the next-most negative integer, then you can use fix and sign like so:

out = fix(x)+sign(x);

For example:

>> x = [-2.1 -2.0 -1.9 -1.1 0 1.1 1.9 2.0 2.1]

x =
   -2.1000   -2.0000   -1.9000   -1.1000         0    1.1000    1.9000    2.0000    2.1000

>> out = fix(x)+sign(x)

out =
    -3    -3    -2    -2     0     2     2     3     3
gnovice
  • 125,304
  • 15
  • 256
  • 359
3

For positive numbers x:

floor(x)+1

The second question is a little ambiguous without the examples, but this may be what you are looking for:

ceil(x)-1

informaton
  • 1,462
  • 2
  • 11
  • 20