Question

How to make the if statement identify only integer results of an equation

eg:

For n=1:240
 a=a+1
 b=a/20

 %try number 1
 If b==1 | 2 | 3 | ... | 12;
 c=c+1 %does not work
 End

 %try number 2
 If b==isinteger(b);
 c=c+1 %does not work
 End

 %try number 3
 d=isinteger(b);
 If d==1;
 c=c+1 %does not work
 End
End

I am only new in Matlab so excuse the simplicity of the question.

Était-ce utile?

La solution

One thing you can do is

abs(b-round(b))<1e-12

or similar, which tests whether b is within 1e-12 of an integer.

If you want to test many numbers b, you can have a vector b, and then

b(abs(b-round(b))<1e-12)

should return the elements of b that are integers.

There is a Matlab command called isinteger, DO NOT use it, it is for determining what type of variable something is, you will be using doubles which are a different data type to integers. Just something to be aware of.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top