![]() |
如何以以下格子树形式表示我的Matlab矩阵值?
我在矩阵中有晶格的值,如图1所示:
[IMG]https://i.stack.imgur.com/FP8md.jpg[/IMG] [B]图1:我的代码在Matlab中当前显示的值格式[/B] 现在,我想以图2所示的格子树的形式来表示这些值(请注意,图2中的值与图1中的值不同,图2仅用于演示目的)。我如何在Matlab中修改代码以使结果看起来像图2所示的树格式? [IMG]https://i.stack.imgur.com/XUDCr.jpg[/IMG] [B]图2:我想在Matlab结果中显示的值格式[/B] 以下是我的代码: 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); [B]回答:[/B] 如果您的目标是将上三角矩阵(如图1所示)重新格式化为非零值以树状结构排列的矩阵(如图2所示),则可以使用函数来完成此操作[URL="http://www.mathworks.com/help/techdoc/ref/spdiags.html"]SPDIAGS[/URL] 。这是一个使用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 [url=https://stackoverflow.com/questions/5453418]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 23:26。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.