このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
if, elseif, else
条件が true の場合ステートメントを実行
構文
ifexpression
statements
elseifexpression
statements
elsestatements
end
説明
例
条件付き代入に if、elseif、else を使用
要素が 1 の行列を作成します。
nrows = 4; ncols = 6; A = ones(nrows,ncols);
行列全体をループして各要素に新しい値を代入します。主対角要素に 2
を、その隣接する対角に -1
を、それ以外のすべてに 0
を代入します。
for c = 1:ncols for r = 1:nrows if r == c A(r,c) = 2; elseif abs(r-c) == 1 A(r,c) = -1; else A(r,c) = 0; end end end A
A = 4×6
2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
配列の比較
A > 0
のような配列の比較演算子を含む式は、結果のすべての要素が非ゼロの場合にのみ true になります。
関数 any
を使用して結果が true のものがあるかどうかをテストします。
limit = 0.75; A = rand(10,1)
A = 10×1
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
0.9575
0.9649
if any(A > limit) disp('There is at least one value above the limit.') else disp('All values are below the limit.') end
There is at least one value above the limit.
配列の等価性のテスト
等価性をテストするには、==
演算子ではなく isequal
を使用して配列を比較します。配列のサイズが異なる場合、==
はエラーになるためです。
2 つの配列を作成します。
A = ones(2,3); B = rand(3,4,5);
size(A)
と size(B)
が同じ場合は配列を連結し、それ以外の場合は警告を表示して空の配列を返します。
if isequal(size(A),size(B)) C = [A; B]; else disp('A and B are not the same size.') C = []; end
A and B are not the same size.
文字ベクトルの比較
strcmp
を使用して文字ベクトルを比較します。等価性のテストに ==
を使用すると、文字ベクトルのサイズが異なる場合にエラーになります。
reply = input('Would you like to see an echo? (y/n): ','s'); if strcmp(reply,'y') disp(reply) end
値の非等価性のテスト
値が非ゼロかどうか判断します。非等価性をテストするには ~=
演算子を使用します。
x = 10; if x ~= 0 disp('Nonzero value') end
Nonzero value
式内での複数条件の評価
値が指定した範囲内にあるか判別します。
x = 10; minVal = 2; maxVal = 6; if (x >= minVal) && (x <= maxVal) disp('Value within specified range.') elseif (x > maxVal) disp('Value exceeds maximum value.') else disp('Value is below minimum value.') end
Value exceeds maximum value.
詳細
式
式には関係演算子 (<
や ==
など) と論理演算子 (&&
、||
、~
など) を含めることができます。論理演算子 and
と or
を使用して複合式を作成できます。MATLAB® は複合式を左から右に評価し、演算子の優先順位の規則に従います。
if...end
ブロックの条件式の中では、論理演算子 &
および |
はショートサーキット演算子として動作します。動作はそれぞれ &&
および ||
と同じです。&&
と ||
は、条件式でもステートメントでも一貫してショートサーキットであるため、式内では &
と |
の代わりに &&
と ||
を使用することをお勧めします。以下に例を示します。
x = 42; if exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') end
式の最初の部分の評価結果は false です。このため MATLAB は未定義の関数エラーの結果になる、式の 2 番目の部分を評価する必要はありません。
ヒント
任意の数の
if
ステートメントを入れ子にすることができます。各if
ステートメントには、end
キーワードが必要です。elseif
キーワード内のelse
の後にスペースを追加しないでください (else if
)。スペースにより、入れ子にされたif
ステートメントが生じ、別のend
キーワードが必要になります。
拡張機能
C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。
HDL コード生成
HDL Coder™ を使用して FPGA 設計および ASIC 設計のための Verilog および VHDL のコードを生成します。
&
演算子と|
演算子をif
ステートメントの条件内で使用しないでください。代わりに、&&
演算子と||
演算子を使用します。HDL Coder™ では、
if
ステートメントの条件に非スカラー式を使用することはできません。代わりに、関数all
または関数any
を使用して logical ベクトルをスカラーに変換します。
スレッドベースの環境
MATLAB® の backgroundPool
を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool
を使用してコードを高速化します。
この関数はスレッドベースの環境を完全にサポートしています。詳細については、スレッドベースの環境での MATLAB 関数の実行を参照してください。
バージョン履歴
R2006a より前に導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)