Effacer les filtres
Effacer les filtres

Error: In an assignment A(I) = B, the number of elements in B and I must be the same.

1 vue (au cours des 30 derniers jours)
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 commentaires
Sap
Sap le 12 Mai 2012
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 le 12 Mai 2012
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

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Mai 2012
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)

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by