Problem with a loop for
Show older comments
Hi,
I'm a student and I need help on this case:
I a matrix V of N = 100 rows and T = 30 columns which describes the value of a fund for 30 years. The next year expenditure that the fund has to do should be the arithmetic mean of the last three years fund's value. This is inside a for loop, so i have to do this for 30 years (T), N times. I tried the code below, but it does'nt work.
for t = 1: T
S(1: N, t +1) = (mean (V (1: N, t: t-2), 2)));
end
Can you help me?
Thanks
Answers (2)
I don't know what you want to do for Year 1 which doesn't have prior year data to average. Are you sure you don't want to start this at Year 4? If so, this is one possibility,
S=conv2(V,[0 1 1 1]/3,'valid')
Azzi Abdelmalek
on 25 Oct 2012
Edited: Azzi Abdelmalek
on 25 Oct 2012
T=30;
N=100;
V=rand(N,T); % Example
out=cell2mat(arrayfun(@(x) mean(V(:,max(x-4,1):max(x-1,1)),2),1:T,'un',0));
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!