How do I test for integers in MATLAB?

前端 未结 7 1408
遥遥无期
遥遥无期 2021-01-04 00:55

I\'m writing a program that will calculate factorials of integers. However, the part I\'m stuck on is if someone enters a non-integer such as 1.3, I\'d like to

7条回答
  •  滥情空心
    2021-01-04 01:40

    assert(isnumeric(input) && round(input) == input, 'That number is not an integer.')

    You could add other checks, (like for positivity) easily as well.

    Edited using isinteger. Thanks @SolarStatistics, I hadn't noticed they added this functionality. Edited back to original answer again as isinteger isn't appropriate (see comments below).

提交回复
热议问题