以下是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))(:)]
请注意使用冒号索引将中间结果展平为列向量。这是一个常见的向量化技巧。
版权所有 © 2024-2025 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2