Checking the decimal numbers

5 views (last 30 days)
inga
inga on 9 Nov 2012
I have a matrix A with one column (5000x1). I want to make an if statement, which would check ONLY the decimal numbers of each row and continue with an action, if it is true. Something like this:
for i=1:5000
if decimals of A(i)=.59333 then ...
end
Is there a simple way of controlling only the decimals? Sorry for this way of writing the code, but I didn't know how else to describe it. Thanks in advance!
Ingvar
  1 Comment
Jan
Jan on 9 Nov 2012
Edited: Jan on 9 Nov 2012
Does "decimal" mean "fractional part"?

Sign in to comment.

Answers (1)

Evan
Evan on 9 Nov 2012
Edited: Evan on 9 Nov 2012
mod(A(i),1)
Should tell you whether or not each element is a decimal number. If it returns a nonzero answer, you have a decimal.
So your code would look something like this:
for i = 1:5000
if mod(A(i),1)
% Your operations here
end
end
The "modulus after division" function can also operate on vectors so, depending on what you need to do in the case of a decimal number, you could maybe even eliminate your looping altogether.
  2 Comments
Evan
Evan on 9 Nov 2012
Ah, okay. It looks like this question addresses the potential issues resulting from floating-point numbers?

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!