Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab嵌套类,从内部引用同一类的对象_Matlab - Fatal编程技术网

Matlab嵌套类,从内部引用同一类的对象

Matlab嵌套类,从内部引用同一类的对象,matlab,Matlab,我试图在matlab中创建一个树类,用于创建决策树。当我尝试运行这个脚本时,当它第一次运行代码时,我可以看到它得到了一个有利的值F和V。但是,在初始化左和右的新节点部分之后,即使我的当前对象也是空的。如何在类中正确嵌套对同一类的引用,使它们不会相互干扰 classdef dtree properties (Access = public) MaxDepth; CurrentDepth; Features; Left; Right; F;

我试图在matlab中创建一个树类,用于创建决策树。当我尝试运行这个脚本时,当它第一次运行代码时,我可以看到它得到了一个有利的值F和V。但是,在初始化左和右的新节点部分之后,即使我的当前对象也是空的。如何在类中正确嵌套对同一类的引用,使它们不会相互干扰

classdef dtree
properties (Access = public) 
    MaxDepth;
    CurrentDepth;
    Features;
    Left;
    Right;
    F;
    V;
end
  methods
      function this=dtree(md, cd, nf, Xtrain, Ytrain)
        % Now the real initialization begins
        this.MaxDepth = md
        this.CurrentDepth = cd;
        this.Features = nf;
        this.train(Xtrain, Ytrain);
      end

      function s = gini(dt, Labels)
        s = 1;
        s = s - (sum(Labels > 0.0) ./ numel(Labels)) ^ 2;
        s = s - (sum(Labels < 0.0) ./ numel(Labels)) ^ 2;
    end

    function train(dt, Xtrain, Ytrain)
        if (size(unique(Ytrain)) == 1 | dt.CurrentDepth > dt.MaxDepth)
            return;
        end

        minGINI = Inf;
        minF = 0;
        minV = Inf;
        for i = dt.Features
            for n = 1:size(Xtrain, 1)
                idx = Xtrain(:, i) > Xtrain(n, i);
                GINI = dt.gini(Ytrain(idx)) + dt.gini(Ytrain(~idx));
                if GINI < minGINI
                    minGINI = GINI;
                    minF = i;
                    minV = Xtrain(n, i);
                end
            end
        end
        dt.F = minF
        dt.V = minV
        lIdx = Xtrain(:, dt.F) > Xtrain(dt.V, dt.F);
        dt.Left = dtree(dt.MaxDepth, dt.CurrentDepth + 1, dt.Features,Xtrain(lIdx, :), Ytrain(lIdx))
        dt.Right = dtree(dt.MaxDepth, dt.CurrentDepth + 1, dt.Features, Xtrain(~lIdx, :), Ytrain(~lIdx));
     end
  end
执行后,当我键入ans时 ans=

具有以下属性的数据树:

    MaxDepth: 1
CurrentDepth: 1
    Features: [4 5 2]
        Left: []
       Right: []
           F: 5
           V: 7
    MaxDepth: 1
CurrentDepth: 1
    Features: [4 5 2]
        Left: []
       Right: []
           F: []
           V: []

在运行train之前,它是一个空对象。

处理类方法时,需要显式地传递并返回该对象

function this = dtree(...)
   this = this.train(...)
end

function dt = train(dt, XTrain, YTrain)
...
end
如果不执行此操作,将不会更新对所拥有对象的引用。您总是需要在Matlab中更新参考