Writing a program that checks if an array's elements are integers.

15 views (last 30 days)
I want to write a program which, given a matrix, will return another matrix (the same size) containing a '1' if the corresponding entry in the input matrix is an integer (0, +-1, +-2, +-3, etc...) and a '0' otherwise.
For example, if given a matrix, say, A:
A(1,:)=[1.0000 9.1338 2.7850];
A(2,:)=[9.0579 2.0000 5.4688];
A(3,:)=[1.2699 0.9754 3.0000].
I would expect the following output:
[1 0 0]
[0 0 0]
[0 0 1].
I tried having a look at isint, but this only seems to handle whole matrices (not individual entries), and when it says that it looks for integers, it doesn't necessarily mean all whole numbers, which is the meaning I want to use.
I could code this easily using for loops, but obviously I'd like a faster alternative if at all possible. When I tried using array operations, I can only seem to get a program to generate a single output (i.e. just a '0' or a '1') for the whole matrix, rather than the element-by-element output that I'm after.
Thanks in advance.
PS I'm using MATLAB R2009a on Windows Vista Home Premium (32-bit).

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 21 Apr 2014
~mod(A,1)
  1 Comment
Thomas
Thomas on 21 Apr 2014
Thank you very much for the quick answer! I'd thought about incorporating mod, but forgot about the tilde operator.
Needless to say, I feel like a bit of an idiot now...

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!