Matlab recursion -


need little understanding happening in function particularly line 7 [fnm1,fnm2] = fibrecurmemo(n-1); don't understand how new variable can declared here in array. example of happening appreciated.

function [fn,fnm1] = fibrecurmemo(n) % computes fibonacci number, f(n), using memoized recursion if n <= 2   fn = 1;   fnm1 = 1; else   [fnm1,fnm2] = fibrecurmemo(n-1);   fn = fnm1 + fnm2; end end 

say start with:

fibrecurmemo(3) %// n 3 

the else statements run (since n > 2):

[fnm1,fnm2] = fibrecurmemo(2); %//statement 1 fn = fnm1 + fnm2; %//statement 2 

before statement 2 can run, fibrecurmemo(2) must first run.


the if statements in fibrecurmemo(2) run (since n <= 2):

fn = 1; fnm1 = 1; 

as result, fibrecurmemo(2) returns 1, 1.


contininuing statement 1 above,

[1,1] = fibrecurmemo(2); %//statement 1 fn = 1 + 1; %//statement 2 

finally,

[2, 1] = fibrecurmemo(3); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -