フィルターのクリア

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

3 ビュー (過去 30 日間)
Francesco Maria
Francesco Maria 2023 年 2 月 13 日
コメント済み: Francesco Maria 2023 年 2 月 15 日
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
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
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.
  1 件のコメント
Francesco Maria
Francesco Maria 2023 年 2 月 15 日
Thanks, sometimes i forget it's better to leave things simple for the sake of readability

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by