将结果附加到for循环matlab中

时间:2012-08-05 02:07:38

标签: matlab for-loop append

我有这个循环(这是一个更大的代码的一部分),它为连续的 SettleEvo 日期输出债券的价格(HW2FPrice)。  如何输出单个HW2FPrice数组而不是单个输出?我希望数组 HW2FPrice Vs. SettleEvo 即可。非常感谢,

for SettleEvo = Settle:datenum('14-Mar-2013');
NumCouponsRemaining = cpncount(SettleEvo, Maturity, Period, Basis, 1, IssueDate); % Coupon payments remaining until maturity. Calculates the number of periods from start to finish.
nPeriods = NumCouponsRemaining;



[CFlowAmounts, CFlowDates] = cfamounts(CouponRate, SettleEvo, Maturity);
CFlowTimes = yearfrac(SettleEvo,CFlowDates);
CFlowZero = interp1(ZeroTimes,ZeroRates,CFlowTimes,'linear','extrap');
CFlowDF_Zero = zero2disc(CFlowZero,CFlowDates,SettleEvo);
Price_Zero = CFlowAmounts*CFlowDF_Zero';
[CFlowAmounts, CFlowDates, TFactors, Factors] = cfamounts(CouponRate,SettleEvo,repmat(Maturity,1,nTrials),Period,Basis);
CFlowDFSim = cumprod(exp(squeeze(-SimZeroRates(2:end,1,:).*stepSize)));

SimZeroRates是一个(nPeriods,nTenors,nTrials)数组;

Price_Ind = sum(CFlowAmounts(:,2:end).*CFlowDFSim',2);
HW2FPrice = mean(Price_Ind)
end

1 个答案:

答案 0 :(得分:1)

HW2FPrices = []; % creates an empty array

for SettleEvo = Settle:datenum('14-Mar-2013');
    % ... whatever other code is in your loop
    HW2FPrice = mean(Price_Ind)
    HW2FPrices(end+1) = HW2FPrice;
end

循环完成后,HW2FPrinces将是一个数组,其中包含您在循环中生成的所有HW2FPrice值。