2017-11-04 79 views
0

MATLAB switchに複数のcaseステートメント(3つのうち2つ)を実行する方法はありますか?または、一連のif文を使用する必要がありますか?私は次のように何かをしたいと思います:MATLAB:スイッチで複数のケースを実行

test = {'test1','test2'} 
switch test 
    case 'test1' 
     disp('test1') 
    case 'test2' 
     disp('test2') 
    case 'test3' 
     disp('test3') 
end 

出力:サイドノートで

test1 
test2 

:異なるケースが同時に実行できるように、そのようなコードを並列化する方法はありますか?

答えて

2

if 1 /複数のケース2 /並列化をテストしたい場合は、より適切です。あなたはそれを平行にしたい場合、あなたは以下を通してそれを行うことができます

if ismember('test1',test) 
    %code 
end 

よう

何か:

testは、あなたのデータであり、caseはすべてpossiblities

parfor(i=1:length(cases)){ %you need to parse the cases not the data 
     if(ismember(case{i},test)){ 
     %code 
     } 

} 
を含むセルであります
2

解決策は、switchを関数に入れて、cellfunを使用することができます。したがって、関数を定義する:

function a = func(test) 
    switch test 
     case 'test1' 
      disp('test1') 
     case 'test2' 
      disp('test2') 
     case 'test3' 
      disp('test3') 
    end 
end 

次いで、testに適用:

cellfun(@func, test) 

結果は次のようになります

test1 
test2 
関連する問題