如何用以下格子树形式表示我的 Matlab 矩阵值?

发布于 2024-10-27 07:29:22 字数 985 浏览 1 评论 0原文

我的矩阵中的晶格值如图 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:

enter image description here
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?:

enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-11-03 07:29:22

如果您的目标是将上三角矩阵(如图 1 所示)重新格式化为非零值以树状结构排列的矩阵(如图 2 所示),那么您可以使用以下函数来完成此操作SPDIAGS。这是使用 5×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

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:

>> 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
许一世地老天荒 2024-11-03 07:29:22

我看到一种仅使用一个 for 循环的解决方案..

function B = newShape(A)

n = size(A,1);
B = zeros(2*n-1,n);
idx0 = n:(2*n):(2*n^2 - n);
B(idx0(1):(2*n-2):(2*n^2-n-1)) = A(1,:);
for i=n:(2*n-2)
    B( idx0(i - n + 2):(2*n-2):(2*n^2-n) ) = A(i-(n-1)+1,i-(n-1)+1:end);
end


end

I see a solution using only one for loop..

function B = newShape(A)

n = size(A,1);
B = zeros(2*n-1,n);
idx0 = n:(2*n):(2*n^2 - n);
B(idx0(1):(2*n-2):(2*n^2-n-1)) = A(1,:);
for i=n:(2*n-2)
    B( idx0(i - n + 2):(2*n-2):(2*n^2-n) ) = A(i-(n-1)+1,i-(n-1)+1:end);
end


end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文