Checking the decimal numbers

조회 수: 5 (최근 30일)
inga
inga 2012년 11월 9일
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
Jan
Jan 2012년 11월 9일
편집: Jan 2012년 11월 9일
Does "decimal" mean "fractional part"?

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

답변 (1개)

Evan
Evan 2012년 11월 9일
편집: Evan 2012년 11월 9일
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
Walter Roberson
Walter Roberson 2012년 11월 9일
Caution, though, that checking exact decimals is usually a mistake: see http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Evan
Evan 2012년 11월 9일
Ah, okay. It looks like this question addresses the potential issues resulting from floating-point numbers?

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

카테고리

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