How do I write an if statement for if a variable is an integer, perform a function.

Please, how do I say this? I want to perform a function if Var(i) is an integer?
for i = 1: 1000
Var(i+1) = Var(i) + 100e-4
if Var(i) % condition for if Var(i) is an integer (whole number). How do I write this?
%Perform a function
end
end

 Réponse acceptée

madhan ravi
madhan ravi le 27 Fév 2019
Modifié(e) : madhan ravi le 27 Fév 2019
if ~rem(Var(i),1)

5 commentaires

if ~isinteger(Var(i))
The isinteger function is not the right tool for this job. It checks whether its input is of an integer type not whether its input contains an integer value. If the input is of an integer type like int8(5) it must contain an integer value, but the reverse is not true since double(3) is an integer value that is not of an integer type.
Oopsie thanks steven! So rem() should do the job.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by