Is it possible to indicate a number with "any" units, tens, or hundreds?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2023-2-13
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?
采纳的回答
John D'Errico
2023-2-13
编辑:John D'Errico
2023-2-13
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!