Switch Case statements MATLAB code in a 'for' loop

1 回表示 (過去 30 日間)
Tahmid Abdullah
Tahmid Abdullah 2020 年 6 月 22 日
回答済み: Steven Lord 2020 年 6 月 22 日
I tried running this code below but I am not getting the output as an array of k but instead the otherwise disp'Code not running') is displayed for 4 times. I don't understand where I'm going wrong or is this method wrong? Any help or advise would be appreciated
Code:
L=[6 7 8 9 10];
k=[];
for i=1:length(L)
switch L(i)
case (L(i)<8)
k(i)= -3;
break;
case (L(i)>8)
k(i)=4;
break;
otherwise
disp('Code not running');
end
end
Output:
Code not running
Code not running
Code not running
Code not running
Code not running

採用された回答

Steven Lord
Steven Lord 2020 年 6 月 22 日
switch / case isn't the right tool to use here. I suggest using if / elseif / else instead.

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by