I'm getting this error("Operands to the || and && operators must be convertible to logical scalar values") for the below code, can anyone help me with this error ?

1 回表示 (過去 30 日間)
function day_diff = day_diff(month1,day1,month2,day2)
days_mn = [31 28 31 30 31 30 31 31 30 31 30 31];
if month1>=1 && month1<=12 && month1==ceil(month1) && month2>=1 && month2<=12 && month2==ceil(month2)...
&& day1>=1 && day1<=days_mn(month1) && day2>=1 && day2<=days_mn(month2) && day1==ceil(day1) && day2==ceil(day2)...
&& isscalar(month1) && isscalar(day1) && isscalar(month2) && isscalar(day2)
days1 = sum(days_mn(1:1:(month1-1))) + day1;
days2 = sum(days_mn(1:1:(month2-1))) + day2;
day_diff =abs(days1 - days2);
else
day_diff = -1;
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 17 日

Move the

isscalar(month1) && isscalar(day1) && isscalar(month2) && isscalar(day2)

to the beginning of the test.

  1 件のコメント
Ranil Fernando
Ranil Fernando 2018 年 4 月 17 日
Thank you for the reply @Walter Roberson, It really helped to get to the right answer.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by