Matlab-arrays

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

MATLAB-配列

MATLABのすべてのデータ型のすべての変数は多次元配列です。 ベクトルは1次元配列であり、マトリックスは2次元配列です。

ベクトルと行列についてはすでに説明しました。 この章では、多次元配列について説明します。 ただし、その前に、特別な種類の配列について説明します。

MATLABの特別な配列

このセクションでは、特別な配列を作成する関数について説明します。 これらのすべての関数では、1つの引数で正方配列が作成され、2つの引数で長方形配列が作成されます。

  • zeros()*関数は、すべてゼロの配列を作成します-

たとえば-

zeros(5)

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

ans =
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
  • ones()*関数はすべて1の配列を作成します-

たとえば-

ones(4,3)

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

ans =
      1     1     1
      1     1     1
      1     1     1
      1     1     1
  • eye()*関数は単位行列を作成します。

たとえば-

eye(4)

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

ans =
      1     0     0     0
      0     1     0     0
      0     0     1     0
      0     0     0     1
  • rand()*関数は、(0,1)に一様に分布した乱数の配列を作成します−

たとえば-

rand(3, 5)

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

ans =
   0.8147    0.9134    0.2785    0.9649    0.9572
   0.9058    0.6324    0.5469    0.1576    0.4854
   0.1270    0.0975    0.9575    0.9706    0.8003

魔方陣

  • 魔方陣*は、要素が行方向、列方向、または斜めに追加されたときに同じ合計を生成する正方形です。
  • magic()*関数は、魔方陣配列を作成します。 それは、正方形のサイズを与える特異な引数を取ります。 引数は3以上のスカラーでなければなりません。
magic(4)

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

ans =
   16     2     3    13
   5    11    10     8
   9     7     6    12
   4    14    15     1

多次元配列

2次元を超える配列は、MATLABでは多次元配列と呼ばれます。 MATLABの多次元配列は、通常の2次元行列の拡張です。

一般に、多次元配列を生成するには、まず2次元配列を作成して拡張します。

たとえば、2次元配列aを作成しましょう。

a = [7 9 5; 6 1 9; 4 3 2]

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

a =
   7     9     5
   6     1     9
   4     3     2

配列_a_は3行3列の配列です。次のような値を提供することで、_a_に3番目の次元を追加できます-

a(:, :, 2)= [ 1 2 3; 4 5 6; 7 8 9]

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

a =

ans(:,:,1) =

   0   0   0
   0   0   0
   0   0   0

ans(:,:,2) =

   1   2   3
   4   5   6
   7   8   9

ones()、zeros()、またはrand()関数を使用して多次元配列を作成することもできます。

例えば、

b = rand(4,3,2)

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

b(:,:,1) =
   0.0344    0.7952    0.6463
   0.4387    0.1869    0.7094
   0.3816    0.4898    0.7547
   0.7655    0.4456    0.2760

b(:,:,2) =
   0.6797    0.4984    0.2238
   0.6551    0.9597    0.7513
   0.1626    0.3404    0.2551
   0.1190    0.5853    0.5060
  • cat()*関数を使用して多次元配列を作成することもできます。 指定された次元に沿って配列のリストを連結します-

cat()関数の構文は-

B = cat(dim, A1, A2...)

どこで、

  • _B_は作成された新しい配列です
  • A1 _、 A2 _、…​ 連結される配列です
  • _dim_は、配列を連結する次元です

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

a = [9 8 7; 6 5 4; 3 2 1];
b = [1 2 3; 4 5 6; 7 8 9];
c = cat(3, a, b, [ 2 3 1; 4 7 8; 3 9 0])

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

c(:,:,1) =
      9     8     7
      6     5     4
      3     2     1
c(:,:,2) =
      1     2     3
      4     5     6
      7     8     9
c(:,:,3) =
      2     3     1
      4     7     8
      3     9     0

配列関数

MATLABは、配列の内容を並べ替え、回転、並べ替え、形状変更、またはシフトするための次の関数を提供します。

Function Purpose
length Length of vector or largest array dimension
ndims Number of array dimensions
numel Number of array elements
size Array dimensions
iscolumn Determines whether input is column vector
isempty Determines whether array is empty
ismatrix Determines whether input is matrix
isrow Determines whether input is row vector
isscalar Determines whether input is scalar
isvector Determines whether input is vector
blkdiag Constructs block diagonal matrix from input arguments
circshift Shifts array circularly
ctranspose Complex conjugate transpose
diag Diagonal matrices and diagonals of matrix
flipdim Flips array along specified dimension
fliplr Flips matrix from left to right
flipud Flips matrix up to down
ipermute Inverses permute dimensions of N-D array
permute Rearranges dimensions of N-D array
repmat Replicates and tile array
reshape Reshapes array
rot90 Rotates matrix 90 degrees
shiftdim Shifts dimensions
issorted Determines whether set elements are in sorted order
sort Sorts array elements in ascending or descending order
sortrows Sorts rows in ascending order
squeeze Removes singleton dimensions
transpose Transpose
vectorize Vectorizes expression

次の例は、上記の機能の一部を示しています。

長さ、寸法、要素数-

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

x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x)      % length of x vector
y = rand(3, 4, 5, 2);
ndims(y)       % no of dimensions in array y
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s)       % no of elements in s

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

ans =  8
ans =  4
ans =  23

配列要素の循環シフト-

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

a = [1 2 3; 4 5 6; 7 8 9]  % the original array a
b = circshift(a,1)         %  circular shift first dimension values down by 1.
c = circshift(a,[1 -1])    % circular shift first dimension values % down by 1
                           % and second dimension values to the left % by 1.

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

a =
   1     2     3
   4     5     6
   7     8     9

b =
   7     8     9
   1     2     3
   4     5     6

c =
   8     9     7
   2     3     1
   5     6     4

配列の並べ替え

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

v = [ 23 45 12 9 5 0 19 17]  % horizontal vector
sort(v)                      % sorting v
m = [2 6 4; 5 3 9; 2 0 1]    % two dimensional array
sort(m, 1)                   % sorting m along the row
sort(m, 2)                   % sorting m along the column

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

v =
   23    45    12     9     5     0    19    17
ans =
   0     5     9    12    17    19    23    45
m =
   2     6     4
   5     3     9
   2     0     1
ans =
   2     0     1
   2     3     4
   5     6     9
ans =
   2     4     6
   3     5     9
   0     1     2

セル配列

セル配列は、各セルが異なる次元とデータ型の配列を格納できるインデックス付きセルの配列です。

*cell* 関数は、セル配列の作成に使用されます。 セル関数の構文は-
C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)

どこで、

  • _C_はセル配列です。
  • _dim_は、セル配列Cの次元を指定するスカラー整数または整数のベクトルです。
  • _dim1、…​ 、dimN_はCの次元を指定するスカラー整数です。
  • _obj_は次のいずれかです-
  • Java配列またはオブジェクト
  • System.StringまたはSystem.Object型の.NET配列

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

c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}

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

c =
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
   [1,3] = Green
   [2,3] =  3
   [1,4] = Yellow
   [2,4] =  4
   [1,5] = White
   [2,5] =  5
}

セル配列のデータへのアクセス

セル配列の要素を参照するには2つの方法があります-

  • セルのセットを参照するために、インデックスを最初のブラケット()で囲む
  • インデックスを中括弧\ {}で囲み、個々のセル内のデータを参照します

インデックスを最初の括弧で囲むと、セルのセットを参照します。

滑らかな括弧内のセル配列インデックスは、セルのセットを指します。

たとえば-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)

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

ans =
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
}

中括弧でインデックスを作成することにより、セルのコンテンツにアクセスすることもできます。

たとえば-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}

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

ans = Blue
ans = Green
ans = Yellow