1

As said in the title, I have a recursive function and I am trying to build a tree data structure out of it to save my results. Every node consists in just one single number. The problem is that when I input the tree to the next call of the function, it seems that only the value of the tree is passed along, not the actual tree. Does anyone know how to pass a reference to the tree instead?

Initial Call:

tree = struct('left', 'empty','right', 'empty','feature','empty');
decisiontree_train(AttributeSet, LabelSet, 50, tree, 'node');

Recursive Function:

function decisiontree_train( data, labels, before_split_purity_percentage, tree, branch )

% a1 is 0, a2 is 1
[ a1_split_data, a2_split_data, a1_split_labels, a2_split_labels, ...
    split_feature ] = decisiontree_split( data, labels );

new_tree = struct('left', 'empty','right', 'empty','feature','empty');
if strcmp(branch, 'left')
    tree.left = new_tree;
    new_tree.feature = split_feature;
elseif strcmp(branch, 'right')
    tree.right = new_tree;
    new_tree.feature = split_feature;
elseif strcmp(branch, 'node')
    tree.feature = split_feature;
    new_tree = tree;
end

[ after_split_purity_percentage ] = decisiontree_classcount( a1_split_labels );
if after_split_purity_percentage < 100 && ...
        after_split_purity_percentage > before_split_purity_percentage
    decisiontree_train(a1_split_data, a1_split_labels, ...
        after_split_purity_percentage, new_tree, 'left');
end

[ after_split_purity_percentage ] = decisiontree_classcount( a2_split_labels );
if after_split_purity_percentage < 100 && ...
        after_split_purity_percentage > before_split_purity_percentage
    decisiontree_train(a2_split_data, a2_split_labels, ...
        after_split_purity_percentage, new_tree, 'right');
end

% add variable to workspace
% assignin('base', 'a1_split_data', a1_split_data)

end
Alessandro
  • 4,000
  • 12
  • 63
  • 131

1 Answers1

3

Unless you use object oriented matlab, there is no pass by reference. While asking a different question, the answers somehow apply to your case as well. If you are using Matlab 2015b or newer, use Matlab OOP and implement your tree using a handle class. If performance isn't a big concern, do the same.

For the likely reason that both isn't true, you have to work around the issue. Matlab uses copy-on-wrote. Thus changing your functions to take your tree structure as a first input argument and returning the modified it isn't a bad idea. In typical cases only very little data is really copied.

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thanks Daniel for the reply, I thought about returning the modified tree however I am calling the function twice within the function itself, so i don't think it would work – Alessandro Dec 02 '15 at 19:22
  • 1
    It would work, just use it in each function call and return it everywhere. – Daniel Dec 02 '15 at 20:33