Integer check

조회 수: 1,203 (최근 30일)
Andy
Andy 2011년 9월 21일
댓글: Phani 2021년 10월 6일
How can i do an integer check in matlab? Say i have a number, a = 1.5, how do i check if it is integer or not? i want to use an if statement:
if non_integer(a) == true
end
so i need to know what to use for "non_integer", which would return a true or false
  댓글 수: 1
Phani
Phani 2021년 10월 6일
I think you can use rem function which throws out the remainder.
if(rem(a,1) ~=0) %checks if a is an integer with help of remainder
....
end

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

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 9월 21일
  댓글 수: 3
Jukka Kaipala
Jukka Kaipala 2016년 7월 27일
Like it should. Inf is not considered an integer, and the same goes for NaN.
Thorsten
Thorsten 2016년 11월 2일
편집: Thorsten 2016년 11월 2일
Like it should not. Because Inf is not an integer, but
floor(inf) == inf
returns true.
So the correct test would be
~isinf(x) & floor(x) == x
Or, as Walter suggests,
mod(x,1) == 0
But this may be all just of theoretical interest.

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

추가 답변 (4개)

Walter Roberson
Walter Roberson 2011년 9월 21일
I suggest you think about floor(), ceil(), fix(), and round()
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 9월 21일
A test that works even for nan and inf and -inf :
mod(x,1) == 0
Walter Roberson
Walter Roberson 2016년 11월 2일
NaN == 0 is false, so in each of those cases, the result of the test would be false, indicating that the values are not integers.
But if you tried the one above, floor(inf)==inf then the result would be true, seemingly indicating that it is an integer when it is not.

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


Fandi Bataineh
Fandi Bataineh 2019년 2월 2일
편집: Fandi Bataineh 2019년 2월 2일
use
floor(x)==ceil(x)
this will be true only for integers
  댓글 수: 1
Sivyer Ge
Sivyer Ge 2019년 2월 24일
Actually this will not work for type 'char'. Do you know other way to identify the difference between char and integer?

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


Manuel Alcazar
Manuel Alcazar 2019년 9월 16일
function [bool,idx] = isint(x)
% Check whether input is integer or not
% Inf and NaN are not integers
if ~isnumeric(x)
error('Input must be a numeric, not a %s.',class(x))
end
bool = (mod(x,1) == 0);
% bool = (round(x) == floor(x)); % Other approach. Fails with Inf and
% NaN.
idx = find(bool);
% Manolín Sept-2019
end

per isakson
per isakson 2019년 9월 16일
function isf = isflint( m )
% floating double only
%
% http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf
% JanSimon: http://www.mathworks.se/matlabcentral/answers/67247-about-isinteger- ...
% command-confusion#answer_78914
assert( isa( m, 'double' ) && isvector( m ) ...
, 'isflint:IllegalInput' ...
, 'The input should be a double vector' )
isf = all( abs( m ) <= flintmax ) ...
&& all( floor( m ) == m ) ;
end
  댓글 수: 1
Stephen23
Stephen23 2019년 9월 16일
This could be trivially vectorized: why force it to return a scalar?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by