首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对递归MatLab函数进行“引用调用”

如何对递归MatLab函数进行“引用调用”
EN

Stack Overflow用户
提问于 2015-12-02 18:10:11
回答 1查看 122关注 0票数 1

正如在标题中所说的,我有一个递归函数,我试图用它构建一个树数据结构来保存我的结果。每个节点仅由一个数字组成。问题是,当我将树输入到函数的下一个调用时,似乎只传递了树的值,而不是实际的树。有人知道如何将引用传递给树吗?

初步呼叫:

代码语言:javascript
复制
tree = struct('left', 'empty','right', 'empty','feature','empty');
decisiontree_train(AttributeSet, LabelSet, 50, tree, 'node');

递归函数:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-02 18:23:09

除非您使用面向对象的matlab,否则不会有引用。当问另一个问题时,the answers somehow apply to your case as well。如果您正在使用Matlab2015b或更高版本,请使用Matlab并使用handle类实现您的树。如果性能不是什么大问题,那就做同样的事情。

由于两者都不是真的原因,你必须解决这个问题。Matlab使用在写的副本.因此,更改函数以将树结构作为第一个输入参数并返回修改后的参数并不是一个坏主意。在典型情况下,只有很少的数据真正被复制。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34049965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档