プログラミングでもっと自在にFreeMatを操ろう無償ソフトで技術計算しよう【プログラミング基礎編】(1)(2/2 ページ)

» 2014年04月25日 10時45分 公開
[伊藤孝宏,MONOist]
前のページへ 1|2       

条件により実行を変える

 ifコマンド、switchコマンドは、条件により実行を変えたい場合に用います。ifは、2方向への分岐で条件を満足する場合、ifの次の行を実行し、満足しない場合は、else以下の行を実行します。一方、switchコマンドは、多方向に分岐できます。分岐の条件は、caseの後に記述し、条件を満足したcase以下の行を実行します。

ifコマンド

 ifコマンドは基本となるコマンドなので、FreeMatのヘルプ記述をそのまま下記に示します。

The if and else statements form a control structure for conditional execution. The general syntax involves an if test, followed by zero or more elseif clauses, and finally an optional else clause:

if conditional_expression_1

statements_1

elseif conditional_expression_2

statements_2

elseif conditional_expresiion_3

statements_3

...

else

statements_N

end

Note that a conditional expression is considered true if the real part of the result of the expression contains all non-zero elements (this strange convention is adopted for compatibility with MATLAB).

 意味は、「ifとelseは記述された条件により処理をコントロールします。一般的な書式は、if文と、その後に複数のelseif文が続き、最後にelse文で構成されます。条件書式の実数部が0でなければ、trueと見なします。これは、MATLABの仕様に合わせたため」となります。

 ifコマンドやelseifコマンドはその後に続く変数、論理式の結果が0でなければ、trueと見なし、ifコマンドに続く行を実行しifコマンドを終了します。0であれば、ifコマンドに続く行を無視し、その次の行に移動します。この際、ifコマンドの後ろには行列でも複素数でも対応し、その際は、全て実数部が0でない要素であれば、trueと見なします。この実数部で評価するのはMATLABの仕様にならっていると言っています。例えば、ex301.mを動作させると、aは要素が1,2,3の行列で0を含まないため、次の行が実行され、「a is non-zero」と表示されます。一方、bは虚数であるため、厳密には0ではありませんが、0と見なされ、else以下の行が実行され、「b is zero」と表示されます。

clear;
a=[1,2,3];
if a
    disp('a is non-zero');
else
    disp('a is zero');
end
b=a*i;
if b
    disp('b is non-zero');
else
    disp('b is zero');
end
ex301.m

>>「ex301.m」ダウンロード

 一般に、ifコマンドの後には、論理式(論理式は式を満足する場合はtrueとして1を、満足しない場合はfalseとして0を返します)、例えば、x>1などが使われますが、計算結果や行列を使うことも可能です。

switchコマンド

 switchコマンドも基本となるコマンドなので、ヘルプの記述をそのまま下記に示します。

The switch statement is used to selective execute code based on the value of either scalar value or a string. The general syntax for a switch statement is

switch(expression)

case test_expression_1

statements

case test_expression_2

statements

otherwise

statements

end

The otherwise clause is optional. Note that each test expression can either be a scalar value, a string to test against (if the switch expression is a string), or a cell-array of expressions to test against. Note that unlike C switch statements, the FreeMat switch does not have fall-through, meaning that the statements associated with the first matching case are executed, and then the switch ends. Also, if the switch expression matches multiple case expressions, only the first one is executed.

 意味は、「switchコマンドは数値あるいは文字列に基づいて処理を選択します。otherwiseはなくとも構いません。C言語とは異なり、FreeMatのswitchは、フォールスルーしません。条件に該当するcaseでは処理を実行した後、その後のcaseやotherwiseを飛ばしてend行まで移動します。そのため、複数のcaseを満足する場合は、最初に条件を満足したcaseのみが実行されます」となります。

 「フォールスルー」とはcaseに該当してもしなくても、「全てのcaseで該当するか」をチェックすることです。C言語では、余分なcaseチェックを省くため、それぞれのcaseの最後にbreakコマンドを入れ、フォールスルーを行わせない工夫が必要です。

 例として、ex302.mを動作させると、文字列変数d='rice'をcaseにあるセル配列と比較します。'rice'は最初のcaseのセル配列内にあるため、c='food'が実行され、rice is foodと表示されます。この場合、3番目のcaseにも'rice'が含まれていますが、最初のcaseを満たしたため、その後のcaseやotherwiseは実行されずにendまで移動しています。

clear;
d='rice';
switch(d)
    case {'rice','bread'}
        c='food';
    case {'red','blue','green'}
        c='color';
    case {'rice','oats'}
        c='plant';
    otherwise
        c='not sure';
end
printf('%s is %s \n',d,c)
ex302.m

>>「ex302.m」ダウンロード

 次回は「forコマンド」について説明します。

参考文献

  • 「MATLABハンドブック」小林一行著、秀和システム刊
  • 「はじめてのFreeMat」赤間世紀著、工学社刊

無償ソフトで技術計算しよう

FreeMat

無償の工学計算ソフトでも、かなり高度な計算ができる!


筆者紹介

伊藤孝宏(いとう・たかひろ)

1960年生。小型モーターメーカーのエンジニア。博士(工学)。専門は流体工学、音・振動工学。現在は、LabVIEWを使って、音不良の計測・診断ソフト、特性自動検査装置などの開発を行っている。



前のページへ 1|2       

Copyright © ITmedia, Inc. All Rights Reserved.