Borrar filtros
Borrar filtros

Is it possible to indicate a number with "any" units, tens, or hundreds?

3 visualizaciones (últimos 30 días)
Hi, i'm new to MATLAB and i'm really just into the basics. I wanted to make a check of a time inserted by an input, so the program can the reiterate the input if the time inserted is wrong or non-existent (in this case time is an integer in the format "hhmm"). The check i've made is long and obsolete and i'd like to shorten it. Is it possible to use a number indicating any units? For example: i want to exclude all numbers between 540 and 549 but i don't want to manually say ">=540 && <550" for all the timing i want to exclude. Thanks a lot for your help
  1 comentario
Rik
Rik el 13 de Feb. de 2023
Why is your proposed solution not acceptable? Can you give examples of the kind of input data you want to parse and what results you expect?

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 13 de Feb. de 2023
Editada: John D'Errico el 13 de Feb. de 2023
There is not really any trivial way to test for what you want, than to just test for a number in that range. And that will require the two tests that you already know how to perform.
Could you extract the units digit, or perhaps the hundreths digit? Well, yes. mod can help. Thus if a number is an integer, then the units digit is just mod(x,10).
And, I suppose the 10's digit of an integer x can be gained by mod(floor(x/10),10). Similarly, the higher order digits can be gained in a similar way.
Is that really any simpler than what you were doing? For example this one test:
mod(floor(x.100),10) == 0
compared to two simpler tests to write? Don't forget that the complexity of code is important too. Can you read what you wrote, next year when you need to debug your code?
Your choice, I guess.
  1 comentario
Francesco Maria
Francesco Maria el 15 de Feb. de 2023
Thanks, sometimes i forget it's better to leave things simple for the sake of readability

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by