Matrix dimensions must agree error in if loop

조회 수: 2 (최근 30일)
Ellen De Jonghe
Ellen De Jonghe 2020년 1월 15일
댓글: Ellen De Jonghe 2020년 1월 15일
Why do I get a matrix dimensions error here?
I'm alson not sure about the num2str parts in the disp. Do I even have to convert day to a string because it is a string right?
day = input('What day is today?', 's');
if day == 'Saturday' | day == 'Sunday'
disp(['Its ' num2str(day) ' ! Its weekend!'])
else
disp(['Its ' num2str(day) ' ! Get to work!'])
end
>> whichDay
What day is today? sunday
Matrix dimensions must agree.
Error in whichDay (line 2)
if day == 'Saturday' | day == 'Sunday'

채택된 답변

Andrei Bobrov
Andrei Bobrov 2020년 1월 15일
편집: Andrei Bobrov 2020년 1월 15일
day = input('What day is today? -> ', 's');
lo = any(strcmpi(day,{'saturday','sunday'}));
if lo
disp(['Its ' day ' ! Its weekend!'])
else
disp(['Its ' day ' ! Get to work!'])
end
Illustration to the error you received:
day = input('What day is today? -> ', 's');
What day is today? -> Sunday
>> day == 'Saturday' | day == 'Sunday'
Matrix dimensions must agree.
>> day == 'Saturday'
Matrix dimensions must agree.
>> day == 'Sunday'
ans =
1×6 logical array
1 1 1 1 1 1
>>
  댓글 수: 1
Ellen De Jonghe
Ellen De Jonghe 2020년 1월 15일
Thanks!
Do you also know why i get the error?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by