How can I test if an input value is an integer?
153 次查看(过去 30 天)
显示 更早的评论
I want to know how to test whether an input value is an integer or not. I have tried using the function isinteger, but I obtain, for example, isinteger(3) = 0. Apparently, any constant is double-precision by Matlab default, and is therefore not recognized as an integer. Can anyone tell me what function I'm supposed to use?
2 个评论
Torsten
2017-2-21
Please explain in further detail the reason why you want to perform the test.
Best wishes
Torsten.
采纳的回答
James Tursa
2017-2-21
E.g.,
>> isaninteger = @(x)isfinite(x) & x==floor(x)
isaninteger =
@(x)isfinite(x) & x==floor(x)
>> isaninteger(3)
ans =
1
>> isaninteger(3.1)
ans =
0
>> isaninteger(inf)
ans =
0
>> isaninteger(nan)
ans =
0
>> isaninteger(uint8(4))
ans =
1
>> isaninteger([-inf -1.2 0 1.3 5 inf nan])
ans =
0 0 1 0 1 0 0
3 个评论
tqml
2020-10-30
I think using equal with floating point values might be error prone.
> mod((1:5)+eps,1)==0
ans =
1×5 logical array
0 1 1 1 1
For double value eps should be aroung 1e-15, so small rounding errors could mess up the check.
Therefore it might be helpful to introduce an additional parameter to control the tolerance and only check if the result is CLOSE to zero (and not equal)
function [b] = is_integer_value(x, tolerance)
%IS_INTEGER_VALUE Returns true if the value is an integer
% Checks if x modulo 1 is close to 0.
if nargin < 2
tolerance = eps;
end
b = ( mod(x,1) < tolerance ) ;
end
Steven Lord
2020-10-30
When you call the eps function with no inputs, it returns the distance from 1 to the next largest number.
The distance from (for example) 2 to the next largest number is greater than eps. When you call eps with an input, it returns the distances from the elements of the input to the next largest numbers.
>> x = 1:5;
>> y = x + eps;
>> y > x
ans =
1×5 logical array
1 0 0 0 0
>> z = x + eps(x);
>> z > x
ans =
1×5 logical array
1 1 1 1 1
You can see the value of eps for each element of x:
>> eps(x)
ans =
Columns 1 through 3
2.22044604925031e-16 4.44089209850063e-16 4.44089209850063e-16
Columns 4 through 5
8.88178419700125e-16 8.88178419700125e-16
更多回答(5 个)
Adam
2017-2-21
I use e.g.
validateattributes( myInteger, { 'double' }, { 'scalar', 'integer' } )
for validating my input arguments. Not so useful if you want a boolean/logical returned though to do something with after, although you can catch the exception that gets thrown but its untidy and inefficient to do that.
0 个评论
randell mullally
2019-7-3
I needed the same thing. these answers are Blaah for me so here was my soloution.
clear;
clc;
A=[3.6,4,4.25,5,6.175,nan,inf];
SizeA=size(A);
B=zeros(SizeA);
for i=1:SizeA(1,2)
if A(i)/round(A(i))==1
B(i)=1;
end
end
AB=[A;B]
AB =
3.6000 4.0000 4.2500 5.0000 6.1750 NaN Inf
0 1.0000 0 1.0000 0 0 0
I suppose if you want to test just one at a time... and let y be the test subject.
y=5;
if y/round(y)==1
fprintf('%3g is an intiger!! hooray\n',y)
else
fprintf('%3g is not an intiger!! sorry\n',y)
end
5 is an intiger!! hooray
testing this with A yeilds good results.
clear;
clc;
A=[3.6,4,4.25,5,6.175,nan,inf];
SizeA=size(A);
B=zeros(SizeA);
for i=1:SizeA(1,2)
if A(i)/round(A(i))==1
B(i)=1;
end
y=A(i);
if y/round(y)==1
fprintf('%3g is an intiger!! hooray\n',y)
else
fprintf('%3g is not an intiger!! sorry\n',y)
end
end
AB=[A;B];
3.6 is not an intiger!! sorry
4 is an intiger!! hooray
4.25 is not an intiger!! sorry
5 is an intiger!! hooray
6.175 is not an intiger!! sorry
NaN is not an intiger!! sorry
Inf is not an intiger!! sorry
If you really really really like this, ALOT!
find me on linkedin and endorse my matlab skill.
Best of luck!
1 个评论
Cris Luengo
2020-7-9
This does not work if the input is 0.
What is wrong with the other answers? They actually work!
Weimin Zhang
2023-2-23
For an array x, perhaps use this:
assert(~any(mod(x,1)),'x must have integer(s) only');
or a function allIntegers = @(x)~any(mod(x,1))
For example,
>> allIntegers([1,2,3])
ans = logical 1
>> allIntegers([1,2.5,3])
ans = logical 0
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!