2012-08-05 183 views
0

我有這個循環(它是一個更大的代碼的一部分),它輸出債券的價格(HW2FPrice)爲連續的SettleEvo日期。 如何讓它輸出一個單一的HW2FPrice數組而不是大量的單個輸出?我想陣列是HW2FPrice VS. SettleEvo。非常感謝,追加for循環matlab

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

你在這裏問的不完全清楚 - 你能否削減代碼並澄清你需要什麼? – Ansari 2012-08-05 06:25:18

+0

我主要想將HW2FPrice存儲在單個輸出中,而不是單個輸出。這對我有好處。剩下的代碼就是爲了展示SettleEvo日期如何與HW2FPrice依賴的其他值相關聯。這是爲了防止有人能夠進一步進行並在輸出中包含給定的每個HW2FPrice的相應SettleEvo日期。非常感謝您的幫助。 – Mary 2012-08-05 12:03:33

回答

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值的數組。

+0

嗨,這個作品很棒。非常感謝。在得到錯誤信息後,我必須將HW2FPrices [end + 1]更改爲HW2FPrices(end + 1)。但它的工作原理與我需要的完全一樣。非常感謝您的幫助。謝謝! – Mary 2012-08-11 10:58:10

+0

@瑪麗「我確實不得不改變......」(搖着空氣中的拳頭)...... JAVA! ......好的,我爲我的後代做了正確的答案。 – mwengler 2012-08-15 18:43:28

+0

道歉..顯示了我認識matlab的水平,給了我多少困惑。我想我把它放在一個「我做到了,是嗎?」有點事。對於具有我的matlab複雜程度的後代而言。再次感謝。 – Mary 2012-08-15 21:24:10