Error: In an assignment A(I) = B, the number of elements in B and I must be the same.
2 次查看(过去 30 天)
显示 更早的评论
The main code below for function Vt creates a 241 x 1 matrix. I want to repeat the calculations and store it in a new matrix that is 241 x 10, so each column has a new set of data repeated.
When I try this as shown below i get the error message? How do i change it to get a 241 x 10 matrix.
for j=1:10
Vt(j) = V*exp((r-(sigma^2)/2)*(0:1:N)'/N*T+ sigma*([0; cumsum(randn(N,1))]/sqrt(N))*sqrt(T));
end
Error: In an assignment A(I) = B, the number of elements in B and I must be
the same.
function Firmvalue = GBM_FV(V,K,T,r,sigma)
%T = 20; % Time until maturity
%r = 0.05; % The risk free interest rate
%sigma = 0.20; % The asset volatility
%V = 100; % Starting asset value
%K = 75; % The principal of the debt
N = 240;
Vt = V*exp((r-(sigma^2)/2)*(0:1:N)'/N*T+ sigma*([0; cumsum(randn(N,1))]/sqrt(N))*sqrt(T));
采纳的回答
Walter Roberson
2012-5-12
It appears to me that inside the exp() you are adding two column vectors of length N+1 each. That part should work fine, and exp() of that should give you a column vector of length N+1
Now you have V*exp(....) and "*" is the matrix multiplication operation. What size is V? We don't know, but the comment suggests that it is a scalar. If so then the result of the matrix multiplication would be a column vector of length N+1. And you then try to store those N+1 elements into a single numeric array location, Vt(j)
Perhaps what you want is to assign to Vt(:,j)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!