For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > Recursive Function Help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Recursive Function Help
Benjamin

2008-01-24, 8:57 am

I have the recursive function timeAvgMean. I would like the
variable m to store the entire vector of m=[4,3.5,3,2.5],
and not just output a single number 2.5. Is there anyway to
do this with a recursive routine. I know I could use a for
loop around timeAvgMean(x,N) for N=1:4 in the main
function, but that would reduntantly call timeAvgMean.

function main()
x=[4;3;2;1];
timeAvgMean(x,4);
end

function m = timeAvgMean(x,N)
if N == 0
m = 0;
else
temp = timeAvgMean(x,N-1);
m = temp+(1/N)*(x(N)-temp)
end
end
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com