Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我在矩阵中有晶格的值,如图1所示:
![]() 现在,我想以图2所示的格子树的形式来表示这些值(请注意,图2中的值与图1中的值不同,图2仅用于演示目的)。我如何在Matlab中修改代码以使结果看起来像图2所示的树格式? ![]() 以下是我的代码: function [price,BLOV_lattice]=BLOV_general(S0,K,sigma,r,T,nColumn) %% Constant parameters del_T=T./nColumn; % where n is the number of columns u=exp(sigma.*sqrt(del_T)); d=1./u; p=(exp(r.*del_T)-d)./(ud); a=exp(-r.*del_T); %% Initializing the lattice Stree=zeros(nColumn+1,nColumn+1); BLOV_lattice=zeros(nColumn+1,nColumn+1); %% Developing the lattice for i=0:nColumn for j=0:i Stree(j+1,i+1)=S0.*(u.^j)*(d.^(ij)); end end for i=0:nColumn BLOV_lattice(i+1,nColumn+1)=max(Stree(i+1,nColumn+1)-K,0); end for i=nColumn:-1:1 for j=0:i-1 BLOV_lattice(j+1,i)=a.*(((1-p).*BLOV_lattice(j+1,i+1))+(p.*BLOV_lattice(j+2,i+1))); end end price=BLOV_lattice(1,1); 回答: 如果您的目标是将上三角矩阵(如图1所示)重新格式化为非零值以树状结构排列的矩阵(如图2所示),则可以使用函数来完成此操作SPDIAGS 。这是一个使用5 x 5矩阵的示例: >> A = triu(reshape(1:25,5,5)) %# A sample upper-triangular matrix A = 1 6 11 16 21 0 7 12 17 22 0 0 13 18 23 0 0 0 19 24 0 0 0 0 25 >> N = size(A,1); %# The size of the rows and columns in A >> B = full(spdiags(spdiags(A),(1-N):2:(N-1),zeros(2*N-1,N))) B = 0 0 0 0 21 0 0 0 16 0 0 0 11 0 22 0 6 0 17 0 1 0 12 0 23 0 7 0 18 0 0 0 13 0 24 0 0 0 19 0 0 0 0 0 25 更多&回答... |
![]() |
![]() |