[ACCEPTED]-How do I test for integers in MATLAB?-integer
You can use the mod
function, which returns 6 the remainder after division. All integers 5 are divisible by 1
. So a good test for non-integer 4 would be
integerTest=~mod(value,1);
This returns 0
if value
is not an integer 3 and 1
if it is. You can then use this as 2 a conditional to reject non-integer user 1 inputs.
Here is another variation (you can see it 2 being used in ISIND function: edit isind.m
):
integerTest = ( x == floor(x) );
On my machine, it 1 is faster than the other proposed solutions:
%# create a vector of doubles, containing integers and non-integers
x = (1:100000)'; %'
idx = ( rand(size(x)) < 0.5 );
x(idx) = x(idx) + rand(sum(idx),1);
%# test for integers
tic, q1 = ~mod(x, 1); toc
tic, q2 = x==double(uint64(x)); toc
tic, q3 = x==floor(x); toc
%# compare results
assert( isequal(q1,q2,q3) )
Timings:
Elapsed time is 0.012253 seconds.
Elapsed time is 0.014201 seconds.
Elapsed time is 0.005665 seconds.
You can cast the value to an integer and 4 back to a double and check the result against 3 the original value:
>> x = 1.3; >> x == double(uint64(x)) ans = 0 >> x = 2; >> x == double(uint64(x)) ans = 1
Interestingly, R.M.'s approach of using 2 MOD runs faster in a loop and the above 1 casting approach runs faster when vectorized:
>> x = rand(100000, 1); >> tic; for ii = 1:100000; ~mod(x(ii), 1); end; toc; Elapsed time is 0.018380 seconds. >> tic; for ii = 1:100000; x(ii) == double(uint64(x(ii))); end; toc; Elapsed time is 0.383020 seconds. >> tic; ~mod(x, 1); toc; Elapsed time is 0.005299 seconds. >> tic; x == double(uint64(x)); toc; Elapsed time is 0.002971 seconds.
assert(isnumeric(input) && round(input) == input, 'That number is not an integer.')
You could add other checks, (like for positivity) easily 4 as well.
Edited using isinteger
. Thanks @SolarStatistics
, I hadn't 3 noticed they added this functionality.
Edited 2 back to original answer again as isinteger
isn't 1 appropriate (see comments below).
As point out by @nibot isinteger tests for the input 3 as an integer TYPE. Instead you could check 2 to see if rounding input returns the same value 1 as input. eg:
assert(abs(round(input)-input))<eps*2,'That number is not an integer.')
for example
>> input=1.3;
>> assert(abs(round(input)-input)<eps*2,'That number is not an integer.')
??? That number is not an integer.
>> input=3;
>> assert(abs(round(input)-input)<eps*2,'That number is not an integer.')
>>
I just wanted to point out that the provided 12 methods all test for whether the input is 11 a Gaussian integer, meaning that the real and imaginary 10 parts are both integers. If you need to care 9 about the imaginary part then you need to 8 deal with it separately.
For my applications, inputs 7 with imaginary components shouldn't be considered 6 a valid integer, so I have this:
function boolResult = fnIsInteger(input)
%validate input
if isempty(input)
error('Input cannot be empty')
elseif ~isnumeric(input)
error('Input must be numeric')
end
boolResult = (imag(input) == 0) & (round(input) == input);
end
Using b3.'s 5 tests:
>> x = rand(100000, 1);
>> tic; for ii = 1:100000; ~mod(x(ii), 1); end; toc;
Elapsed time is 0.003960 seconds.
>> tic; for ii = 1:100000; fnIsInteger(x(ii)); end; toc;
Elapsed time is 0.217397 seconds.
>> tic; ~mod(x, 1); toc;
Elapsed time is 0.000967 seconds.
>> tic; fnIsInteger(x); toc;
Elapsed time is 0.003195 seconds.
The looped call is quite a bit slower 4 mostly due to the function overhead. Replacing 3 the arithmetic expression with ~mod(dataInput, 1) will 2 make it only 50% faster than the code that 1 checks for imaginary parts.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.