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));
  3 个评论
Sap
Sap 2012-5-12
sigma, r and T are all scalars and mentioned above. N=240.
I wanted Vt to be the 241 * 10 matrix. I don't mind giving the matrix a different name either.
Sap
Sap 2012-5-12
The error appears after the following line:
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by