正如在标题中所说的,我有一个递归函数,我试图用它构建一个树数据结构来保存我的结果。每个节点仅由一个数字组成。问题是,当我将树输入到函数的下一个调用时,似乎只传递了树的值,而不是实际的树。有人知道如何将引用传递给树吗?
初步呼叫:
tree = struct('left', 'empty','right', 'empty','feature','empty');
decisiontree_train(AttributeSet, LabelSet, 50, tree, 'node');递归函数:
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发布于 2015-12-02 18:23:09
除非您使用面向对象的matlab,否则不会有引用。当问另一个问题时,the answers somehow apply to your case as well。如果您正在使用Matlab2015b或更高版本,请使用Matlab并使用handle类实现您的树。如果性能不是什么大问题,那就做同样的事情。
由于两者都不是真的原因,你必须解决这个问题。Matlab使用在写的副本.因此,更改函数以将树结构作为第一个输入参数并返回修改后的参数并不是一个坏主意。在典型情况下,只有很少的数据真正被复制。
https://stackoverflow.com/questions/34049965
复制相似问题