行列の要素を取り出したり、行列の情報を取得する方法について。
単一の要素を取り出す
要素の位置を指定 例:Aの(2,2)要素を取り出します。
1 2 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span>]; B = A(<span class= "hljs-number" >2</span>, <span class= "hljs-number" >2</span>) |
末尾の要素を指定 end を用いることで行または列の末端の要素を選択できます。 行列の要素数が既知でない場合など、末尾の要素番号がわからない場合などに便利です。
1 2 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span>]; B = A(<span class= "hljs-number" >2</span>, end ) % Aの<span class="hljs-number">2</span>行目、最終列の要素を選択 |
最大要素 max() を用いることで行列の最大要素を選択できます。 Aがベクトルの場合はmax(A)でAの要素のうち最大の要素が得られます。 Aが行列の場合、max(A) はAの列をベクトルとして取り扱い、各列の最大要素を含む行ベクトルを返します。
1 2 3 4 5 6 | A1 = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >7</span>]; A2 = max(A1) % A2 = <span class="hljs-number">7</span> B1 = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >8</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >2</span>]; B2 = max(B1) % B2 = [<span class="hljs-number">4</span> <span class="hljs-number">8</span> <span class="hljs-number">3</span>] 各列の中で最大の値が返される B3 = max( max(B1) ) % B3 = <span class="hljs-number">8</span> |
最小要素 min() を用いることで行列の最小要素を選択できます。 Aがベクトルの場合はmax(A)でAの要素のうち最小の要素が得られます。 Aが行列の場合、max(A) はAの列をベクトルとして取り扱い、各列の最小要素を含む行ベクトルを返します。
1 2 3 4 5 6 | A1 = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >7</span>]; A2 = min(A1) % A2 = <span class="hljs-number">1</span> B1 = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >8</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >2</span>]; B2 = min(B1) % B2 = [<span class="hljs-number">1</span> <span class="hljs-number">4</span> <span class="hljs-number">2</span>] 各列の中で最小の値が返される B3 = min( min(B1) ) % B3 = <span class="hljs-number">1</span> |
複数の要素を取り出す
特定の行または列を全て取り出す コロン(全称記号)用いることで特定の行または列を全て取り出すことが出来ます。
1 2 3 4 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span>; <span class= "hljs-number" >7</span> <span class= "hljs-number" >8</span> <span class= "hljs-number" >9</span>]; B1 = A(:, <span class= "hljs-number" >2</span>) % <span class="hljs-number">2</span>列目の全ての要素 B2 = A(<span class= "hljs-number" >3</span>, :) % <span class="hljs-number">3</span>行目の全ての要素 B3 = A([<span class= "hljs-number" >1</span> <span class= "hljs-number" >3</span>], :) % <span class="hljs-number">1</span>行目および<span class="hljs-number">3</span>行目の全ての要素 |
特定の範囲の要素を取り出す コロン(全称記号)用いることで特定の範囲の要素を取り出すことが出来ます。
1 2 3 4 5 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span>; <span class= "hljs-number" >7</span> <span class= "hljs-number" >8</span> <span class= "hljs-number" >9</span>]; B1 = A(<span class= "hljs-number" >1</span>:<span class= "hljs-number" >2</span>, <span class= "hljs-number" >2</span>) % <span class="hljs-number">2</span>列目の、<span class="hljs-number">1</span>行目から<span class="hljs-number">2</span>列目の要素 B2 = A(<span class= "hljs-number" >2</span>:<span class= "hljs-number" >3</span>, <span class= "hljs-number" >1</span>:<span class= "hljs-number" >2</span>) % <span class="hljs-number">2</span>から<span class="hljs-number">3</span>行目の、<span class="hljs-number">1</span>から<span class="hljs-number">2</span>列目の要素 B3 = A([<span class= "hljs-number" >1</span> <span class= "hljs-number" >3</span>], <span class= "hljs-number" >2</span>:<span class= "hljs-number" >3</span>) % <span class="hljs-number">1</span>行目および<span class="hljs-number">3</span>行目の、<span class="hljs-number">2</span>から<span class="hljs-number">3</span>列目の要素 C = diag(A) % |
行列の情報を得る
size() 行列の各次元のサイズを取得できます。
1 2 3 4 5 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span>; <span class= "hljs-number" >4</span> <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span>]; D = size(A) R = size(A, <span class= "hljs-number" >1</span>) C = size(A, <span class= "hljs-number" >2</span>) [m n] = size(A) |
length() 行列の最大次元の要素数を得られます。
1 2 3 4 | A = <span class= "hljs-number" >3</span>:<span class= "hljs-number" >6</span>; L = length(A) B = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >4</span>; <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span> <span class= "hljs-number" >7</span> <span class= "hljs-number" >8</span>]; L = length(B) |
any() 配列中に非ゼロの要素が1つでもあれば1を、そうでなければ0を返します。
1 2 3 4 | A = [<span class= "hljs-number" >0</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >2</span>]; B = [<span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >1</span>; <span class= "hljs-number" >2</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span>]; any(A) % 配列がベクトルの場合、ベクトル中に非ゼロの要素が存在すれば1、そうでなければ0 any(B) % 配列が行列の場合、各列ベクトルについて値を返す |
all() 全ての要素が非ゼロなら1を、そうでなければ0を返します。
1 2 3 4 | A = [<span class= "hljs-number" >0</span> <span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span>]; B = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >1</span>]; all(A) all(B) |
find() 非ゼロ要素のインデックスを返します。
1 2 3 4 5 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >0</span>]; find(A) B = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >1</span>; <span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >2</span>]; [x, y] = find(B) |
numel() 配列に含まれる要素の数を返します。
1 2 | A = [<span class= "hljs-number" >1</span> <span class= "hljs-number" >2</span> <span class= "hljs-number" >3</span> <span class= "hljs-number" >4</span>; <span class= "hljs-number" >5</span> <span class= "hljs-number" >6</span> <span class= "hljs-number" >7</span> <span class= "hljs-number" >8</span>]; mean = sum( A(:) ) / numel(A) % Aの全要素の平均を計算 |
isempty() 配列が空の配列の場合は1を、そうでなければ0を返します。
1 2 3 4 5 6 7 8 | <span class= "hljs-attr" >A</span> = <span class= "hljs-number" >1</span><span class= "hljs-comment" >;</span> <span class= "hljs-attr" >B</span> = [<span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span>]<span class= "hljs-comment" >;</span> <span class= "hljs-attr" >C</span> = []<span class= "hljs-comment" >;</span> <span class= "hljs-section" >[isempty(A) isempty(B) isempty(C)]</span> <span class= "hljs-attr" >A</span> = [<span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span> <span class= "hljs-number" >0</span>]<span class= "hljs-comment" >;</span> <span class= "hljs-attr" >F</span> = find(A)<span class= "hljs-comment" >;</span> <span class= "hljs-section" >[isempty(A) isempty(F)]</span> |
isvector() 配列がベクトルなら1を、そうでなければ0を返します。
1 2 3 4 | <span class= "hljs-attr" >A</span> = <span class= "hljs-number" >1</span><span class= "hljs-comment" >;</span> <span class= "hljs-attr" >B</span> =[<span class= "hljs-number" >1</span> <span class= "hljs-number" >0</span>]<span class= "hljs-comment" >;</span> <span class= "hljs-attr" >C</span> = zeros(<span class= "hljs-number" >2</span>)<span class= "hljs-comment" >;</span> <span class= "hljs-section" >[isvector(A) isvector(B) isvector(C)]</span> |
コメント