Matlab-colon-notation

提供:Dev Guides
移動先:案内検索

MATLAB-コロン表記

  • colon(:)*は、MATLABで最も有用な演算子の1つです。 これは、ベクトル、添え字配列を作成し、*反復を指定*するために使用されます。

あなたは1から10までの整数を含む行ベクトルを作成したい場合は、書き込みます-

1:10

MATLABは、ステートメントを実行し、1から10までの整数を含む行ベクトルを返します-

ans =

   1    2    3    4    5    6    7    8    9   10

あなたは、例えば、1以外の増分値を指定したい場合-

100: -5: 50

MATLABはステートメントを実行し、次の結果を返します-

ans =
   100    95    90    85    80    75    70    65    60    55    50

別の例を見てみましょう-

0:pi/8:pi

MATLABはステートメントを実行し、次の結果を返します-

ans =
   Columns 1 through 7
      0    0.3927    0.7854    1.1781    1.5708    1.9635    2.3562
   Columns 8 through 9
      2.7489    3.1416

コロン演算子を使用して、インデックスのベクトルを作成し、配列の行、列、または要素を選択できます。

次の表は、この目的のためのその使用法を説明しています(マトリックスAを持ってみましょう)-

Format Purpose
A(:,j) is the jth column of A.
A(i,:) is the ith row of A.
A(:,:) is the equivalent two-dimensional array. For matrices this is the same as A.
A(j:k) is A(j), A(j+1),…​,A(k).
A(:,j:k) is A(:,j), A(:,j+1),…​,A(:,k).
A(:,:,k) is the kth page of three-dimensional array A.
A(i,j,k,:) is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:) is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.

スクリプトファイルを作成し、その中に次のコードを入力します-

A = [1 2 3 4; 4 5 6 7; 7 8 9 10]
A(:,2)      % second column of A
A(:,2:3)    % second and third column of A
A(2:3,2:3)  % second and third rows and second and third columns

あなたがファイルを実行すると、次の結果が表示されます-

A =
      1     2     3     4
      4     5     6     7
      7     8     9    10

ans =
      2
      5
      8

ans =
      2     3
      5     6
      8     9

ans =
      5     6
      8     9