行列と配列の作成
この例では、MATLAB を使用して配列と行列を作成する基本的な手法を説明します。行列と配列は、MATLAB における情報とデータの基本的な表現です。
複数の要素を含む単一行の配列を作成するには、要素をコンマ (,) またはスペースのいずれかで区切ります。このタイプの配列を行ベクトルと呼びます。
disp('Create an array with four elements in a single row:') disp('>> a = [1 2 3 4]') a = [1 2 3 4]
Create an array with four elements in a single row: >> a = [1 2 3 4] a = 1 2 3 4
複数の要素を含む単一列の配列を作成するには、要素をセミコロン (;) で区切ります。このタイプの配列を列ベクトルと呼びます。
disp('Create an array with four elements in a single column:') disp('>> a = [1; 2; 3; 4]') a = [1; 2; 3; 4]
Create an array with four elements in a single column: >> a = [1; 2; 3; 4] a = 1 2 3 4
複数の行をもつ行列を作成するには、行をセミコロンで区切ります。
disp('Create a matrix with three rows and three columns:') disp('>> a = [1 2 3; 4 5 6; 7 8 9]') a = [1 2 3; 4 5 6; 7 8 9]
Create a matrix with three rows and three columns: >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9
等間隔の配列を作成するには、':' 演算子を使用して始点と終点を指定します。
disp('Create an array that starts at 1, ends at 9, with each element separated by 2:') disp('>> x = 1:2:9') x = 1:2:9
Create an array that starts at 1, ends at 9, with each element separated by 2: >> x = 1:2:9 x = 1 3 5 7 9
行列を作成する別の方法は、ones、zeros、rand などの関数を使用することです。
disp('Create a 1-by-5 matrix of 0''s:') disp('>> z = zeros(1, 5)') z = zeros(1, 5)
Create a 1-by-5 matrix of 0's: >> z = zeros(1, 5) z = 0 0 0 0 0