Storing output of for loop on a matrix

1 回表示 (過去 30 日間)
Catarina
Catarina 2014 年 12 月 4 日
コメント済み: Catarina 2014 年 12 月 4 日
Hello! I'm new to Matlab and I'm having difficulties with a for loop. So I have a matrix with 3453 rows which correspond to different observations of interest rates. The columns (30) are their maturities. I have used a for loop to take each of these rows of the matrix and calculate what I need (which involves transforming that interest rate row into a cash flow matrix of 30x30) but the problem is I can't seem to be able to then store the output of all the 3453 observations in a new matrix? It only stores the output of the last observation.
Here is what I have:
s=length(swaps);
m=30;
CFlast = eye(30,30);
for t = 1:s
cashflows = repmat(swaps(t,:),30,1);
CF = cashflows + CFlast;
for i = 1:30
for j = 1:30
if i>j
CF(i,j) = 0;
end
end
end
CF = transpose(CF);
discount = zeros(m,s);
discount = CF\ones(30,1);
end
I've seen previous posters say that in these cases an index should be added - like discount(t) = ... - but when I do that it tells me the matrix dimensions do not match. Can someone please help me? Thank you for taking the time to read and reply!! :)

採用された回答

Thorsten
Thorsten 2014 年 12 月 4 日
編集済み: Thorsten 2014 年 12 月 4 日
discount(:,:,t) = CF\ones(30,1);
  1 件のコメント
Catarina
Catarina 2014 年 12 月 4 日
Thank you! You are a life saver! I adjusted this to discount(:,t) and it works perfectly!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by