Array indices must be positive integers or logical values. Error in untitled4 (line 9) max(num)=0;

2 ビュー (過去 30 日間)
Moises
Moises 2022 年 9 月 27 日
回答済み: Walter Roberson 2022 年 9 月 27 日
syms a b x
num=input('Your student number is:');
m=max(num);
max(num)=0;
n=0;

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 27 日
The syms is unused by the remainder of the code.
You are not validating that the user is entering a scalar positive integer.
You are taking max() of a scalar integer, which is going to return the number itself.
Unless, that is, you are expecting that the user is going to enter a vector of digits?? If so then one of the digits could be 0, and using 0 as an index into the new variable max you are creating would be an error.
Have you considered looking at the second output of max() ?
And we strongly recommend against using either max or sum as the names of variables: it rarely ends well for anyone.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by