[英]matlab Subscripted assignment dimension mismatch.
I had built this script but i can't find the problem Matrix in it. 我已经构建了此脚本,但是在其中找不到问题矩阵。 this is my script for reply a call standard option: 这是我的用于回复呼叫标准选项的脚本:
N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;
sigma=0.25;
for i=1:N
ttomat(i)=(N-i+1)*dt; %+1 serve per aggiustare il tempo
d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
d2(i)=d1(i)-sigma*sqrt(ttomat(i));
Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
alpha(i,:)=normcdf(d1(i)); %delta della Call
beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));
end
You have to initialize/pre-allocate the result, which gets calculated in loops. 您必须初始化/预分配结果,该结果将在循环中进行计算。 In your code, you have not pre-allocated the results. 在您的代码中,您尚未预分配结果。 Pre-allocation helps you in achieving your result fast. 预分配可帮助您快速实现结果。 It is always a best practice to pre-allocate the required variables. 预先分配所需变量始终是最佳实践。 Check the below code, is it working for you? 检查以下代码,它对您有用吗?
N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;
sigma=0.25;
S = rand(N,1) ;
ttomat = zeros(1,N) ;
d1 = zeros(1,N) ;
d2 = zeros(1,N) ;
Call = zeros(1,N) ;
alpha = zeros(1,N) ;
beta = zeros(1,N) ;
for i=1:N
ttomat(i)=(N-i+1)*dt; %+1 serve per aggiustare il tempo
d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
d2(i)=d1(i)-sigma*sqrt(ttomat(i));
Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
alpha(i,:)=normcdf(d1(i)); %delta della Call
beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.