Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-14, 20:46   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何以以下格子树形式表示我的Matlab矩阵值?

我在矩阵中有晶格的值,如图1所示:

图1:我的代码在Matlab中当前显示的值格式

现在,我想以图2所示的格子树的形式来表示这些值(请注意,图2中的值与图1中的值不同,图2仅用于演示目的)。我如何在Matlab中修改代码以使结果看起来像图2所示的树格式?

图2:我想在Matlab结果中显示的值格式

以下是我的代码:

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

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 03:00


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.