finding similar numbers and rounding to integers

1 visualización (últimos 30 días)
AA
AA el 17 de Feb. de 2015
Respondida: Sad Grad Student el 18 de Feb. de 2015
Suppose I have 8 variables and I want to run an If function if at least two of those variables are equal to one. All variables are real numbers and should be converted to integers meaning 1.8 or 1.2 should be rounded to 1. How can I implement this as a function?
x1=22, x2=3.3, x3=1.2 x4=1 x5=1.7 x6=44 x7=6 x8=4
  2 comentarios
John D'Errico
John D'Errico el 17 de Feb. de 2015
It is time to learn how to use a vector instead of numbered variables.
Regardless, 1.8 does not round to 1. Floor or fix might be a better choice of words, IF that is your intention.
Roger Stafford
Roger Stafford el 18 de Feb. de 2015
Presuming you have followed John's good advice and placed your eight values in a single vector, which I will call v, do this:
t = sum(round(v)==1)>=2; % Or perhaps you meant 'floor' instead of 'round'
The variable t can be used in the if statement.

Iniciar sesión para comentar.

Respuesta aceptada

Sad Grad Student
Sad Grad Student el 18 de Feb. de 2015
Completely agree with John. But just in case:
if length(find(fix([x1 x2 x3 x4 x5 x6 x7 x8])==1))>=2
...
condition
...
end

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by