Saving values in a recursive function

3 Ansichten (letzte 30 Tage)
Quinn
Quinn am 18 Nov. 2014
Beantwortet: Guillaume am 18 Nov. 2014
Hi! I am given a nested structure (root) that I need to analyze.
The structure has either a branch ( b1, b2, .., bn), which have other structures in it or a/p/or l which has a number.
I need to sum up the whole numbers of a, p and l in the stricture respectively.
While I was working on my code, I found out whenever the recursion occurred, the sum I found was reset to 0, so I could not sum the numbers at all. I tried to give an index but it could not solve the problem as well (I think I did something wrong)
I have tried several times, and this is currently what I have in my m file. I understand the while loop is wrong, but I want to get a gimps of the idea of how I can keep my values when the recursion occurs. Thank you :)
function [ branch ] = tree2( root );
Cn=fieldnames(root);
k=0;
if exist('f')==0
f=1
end
while f>0
for i=1:length(Cn); eval(['branch=root.' Cn{i}]);
if Cn{i}(1)=='b'
k(f+1)=k(f)+1
elseif Cn{i}=='a'
a(f+1)=a(f)+branch
elseif Cn{i}=='p'
p(f+1)=p(f)+branch
elseif Cn{i}=='l'
l(f+1)=l(f)+branch
end
f=f+1
s=isstruct(branch);
if s==1;
tree2(branch)
end
end
end
end

Antworten (1)

Guillaume
Guillaume am 18 Nov. 2014
The main problem is with the way you've implemented the recursion.
All variables declared in a function are local to the particular invocation of the function (unless declared global or persistent). Therefore, each time you call you recurse into tree2, a new set of k, a, p, and l is calculated. Since you don't return them at the end of tree2, their value is lost.
Thus, you need to change your function signature to:
function [a, b, p, l] = tree2(root); %I've changed k to b. Why was it called k anyway?
Then every time you invoke the recursion you add the return values to your local a, b, ...:
[branch_a, branch_b, branch_p, branch_l] = tree2(branch);
a = a + branch_a;
b = b + branch_b;
p = p + branch_p;
l = l + branch_l;
This also greatly simplifies your if elseif. You just add 1 to the relevant variable whenever you encounter a leaf/branch.
if Cn{i}(1) == 'b'
b = b+1;
elseif ...
This should answer your question. Now, there are a few odd things about your code:
1. I find it much easier to read
if ~exist('f')
than
if exist('f') == 0
they're equivalent, but the former I read if not exist. That carries the intent better.
In any case, f is a local variable, so it never exists at that point of the code, so you might as well get rid of the if.
I don't see the point of the while loop anyway. Particularly as it'll never end since f is always > 0 as it starts at 1 and increases from there.
Finally, don't use eval to access the fields of a structure, use dynamic field names:
branch = root.(Cn{i});

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by