如何用以下格子树形式表示我的 Matlab 矩阵值?
我的矩阵中的晶格值如图 1 所示:
图 1:当前在 Matlab 中为我的代码显示的值的格式
现在我想以格子树的形式表示这些值,如图 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)./(u-d);
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.^(i-j));
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);
I have my values of the lattice in the matrix as shown in fig 1:
Fig 1: Format of values as currently displayed in Matlab for my code
Now I would like to represent these values in form of lattice tree as shown in the figure 2 (Note the values in figure 2 are not same as in figure 1, and fig 2 is just for demonstration purpose). How could I modify my code in Matlab in order to have result that looks like the tree format shown in figure 2?:
Fig 2: Format of values as I want to be displayed in my Matlab result
Following is my code:
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)./(u-d);
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.^(i-j));
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是将上三角矩阵(如图 1 所示)重新格式化为非零值以树状结构排列的矩阵(如图 2 所示),那么您可以使用以下函数来完成此操作SPDIAGS。这是使用 5×5 矩阵的示例:
If your goal is to reformat an upper-triangular matrix (as shown in figure 1) into a matrix with the non-zero values arranged in a tree-like structure (as shown in figure 2), then you can accomplish this using the function SPDIAGS. Here's an example using a 5-by-5 matrix:
我看到一种仅使用一个 for 循环的解决方案..
I see a solution using only one for loop..