Is it possible to indicate a number with "any" units, tens, or hundreds?
조회 수: 3 (최근 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!