matlab 下标赋值维度不匹配。
matlab Subscripted assignment dimension mismatch.
我已经构建了这个脚本,但我在其中找不到问题 Matrix。
这是我回复呼叫标准选项的脚本:
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
您必须 initialize/pre-allocate 循环计算的结果。在您的代码中,您没有预先分配结果。预分配可帮助您快速获得结果。预先分配所需的变量始终是最佳做法。检查下面的代码,它对你有用吗?
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
我已经构建了这个脚本,但我在其中找不到问题 Matrix。 这是我回复呼叫标准选项的脚本:
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
您必须 initialize/pre-allocate 循环计算的结果。在您的代码中,您没有预先分配结果。预分配可帮助您快速获得结果。预先分配所需的变量始终是最佳做法。检查下面的代码,它对你有用吗?
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