2

Pythonでは、次のようなことができます

the_weather_is = 'sunshiny'

bad_mood = {'dreary', 'drizzly', 'flawy', 'blustery', 'thundery'}

if the_weather_is in bad_mood:
    print 'Stay at home...'
else:
    print 'All fine...'

MATLAB に相当するものはどのように見えるでしょうか。つまり、文字列 (オプション) のリストがあり、それが にあるかどうかをチェックstringlistますか?

実際、MATLABでリストとして何を使用できるかさえわかりません。セルアレイ?

4

2 に答える 2

4

bad_moodではなくlistセル配列です。

ismember関数を使用the_weather_isして、bad_moodセル配列内にあるかどうかを確認できます。

ismember(the_weather_is, bad_mood)

別の解決策 ( Benoit_11 の回答から) は、strcmpfunctionと組み合わせてanyfunctionを使用することです。

any(strcmp(the_weather_is, bad_mood))

strcmpthe_weather_iscell 配列の各文字列と比較しbad_mood、logical 配列を返します。any論理配列を返したチェックには、少なくとも 1 つのtrue値が含まれています。

于 2014-08-22T12:36:06.447 に答える
2

strcmp を使用して、the_weather_is が cell 配列 bad_mood の一部であるかどうかを確認できます。

the_weather_is = 'sunshiny';

bad_mood = {'dreary', 'drizzly', 'flawy', 'blustery', 'thundery'};


if any(strcmp((bad_mood),the_weather_is))
    disp( 'Stay at home...')
else
    disp( 'All fine...')

end
于 2014-08-22T12:38:22.537 に答える