我以前从未使用过Matlab,我正在尝试将这段代码转换为Python,但我不确定我是否真的了解发生了什么。代码如下:
% Calculate the price at all interior nodes
offsetConstants = [aj(1); cj(end)];
for i = N:-1:1
price(2:end-1,i) = A*price(2:end-1,i+1);
% Offset the first and last terms
price([2 end-1],i) = price([2 end-1],i) + ...
offsetConstants.*price([1 end],i+1);
到目前为止,我一直在使用http://mathesaurus.sourceforge.net/matlab-numpy.html来浏览它,但我仍然对某些部分感到迷茫。这是我目前掌握的情况
OffsetConstants正在使用来自aj和cj的条目创建矩阵
For循环是取除第一行和最后一行以外的所有值的价格,并将其乘以A,然后在第一行和最后一行相加
有人能给我解释一下吗
创建2个项的向量
offsetConstants
。 一个是aj
中的第一项,另一个是cj
中的最后一项。你完全正确反向样式看起来像是要反向传播
price
中的列。 但要注意范围i+1
上升到N+1
,我猜price
矩阵有那么多列从列
N+1
开始,取除第一项和最后一项之外的整列,然后乘以A
,A
即标量矩阵或(N+1)x(N+1)
矩阵。 然后将产品存储到N
列中最后,取第
N+1
-列的第一个和最后一个项,按元素将其乘以offsetConstants
,然后将它加到第二个和最后第二个项上重复此过程,直到获取列#2并放回列#1
注:
price([2 end-1],i)
相当于[price(2,i),price(end-1,i)]
,而不是price(2:end-1,i)
相关问题 更多 >
编程相关推荐