19.7 Examples

Last-modified: 2025-03-04 (火) 21:12:57

19.7 例

以下は、Octave の実際のユーザーから寄せられたベクトル化に関する質問とその解決策の例です。

ベクトルの場合A、次のループ

n = length (A) - 1;
B = zeros (n, 2);
for i = 1:n
 ## this will be two columns, the first is the difference and
 ## the second the mean of the two elements used for the diff.
 B(i,:) = [A(i+1)-A(i), (A(i+1) + A(i))/2];
endfor

次のようなワンライナーに変換できます。

B = [diff(A)(:), 0.5*(A(1:end-1)+A(2:end))(:)]

コロン インデックスを使用して中間結果を列ベクトルに平坦化していることに注意してください。これは一般的なベクトル化のトリックです。