如何在 Matlab 中创建高效的实例变量变异器?

发布于 2024-09-01 20:02:02 字数 1121 浏览 8 评论 0原文

以前,我按如下方式实现了变异器,但是它在我正在研究的递归 OO 算法上运行得非常慢,我怀疑这可能是因为我在每个函数调用上复制对象......这是正确的吗?

%% Example Only
obj2 = tripleAllPoints(obj1)
    obj.pts = obj.pts * 3;
    obj2 = obj1
end

然后,我尝试在不使用输出对象的情况下实现变异器...但是,在 MATLAB 中我似乎无法执行此操作 - 由于范围问题,更改不会“粘住”?

%% Example Only
tripleAllPoints(obj1)
    obj1.pts = obj1.pts * 3;
end

出于应用目的,下面是我的代码的极其简化的版本(使用面向对象和递归)。

classdef myslice

properties
    pts     % array of pts
    nROW % number of rows
    nDIM % number of dimensions
    subs    % sub-slices
end % end properties

methods
    function calcSubs(obj)
        obj.subs = cell(1,obj.nROW);
        for i=1:obj.nROW
            obj.subs{i} = myslice;
            obj.subs{i}.pts = obj.pts(1:i,2:end);
        end
    end

    function vol = calcVol(obj)
      if obj.nROW == 1
         obj.volume = prod(obj.pts);
      else
         obj.volume = 0;
         calcSubs(obj);
         for i=1:obj.nROW
                obj.volume = obj.volume + calcVol(obj.subs{i});
         end
      end
    end

end % end methods

end % end classdef

Previously, I implemented mutators as follows, however it ran spectacularly slowly on a recursive OO algorithm I'm working on, and I suspected it may have been because I was duplicating objects on every function call... is this correct?

%% Example Only
obj2 = tripleAllPoints(obj1)
    obj.pts = obj.pts * 3;
    obj2 = obj1
end

I then tried implementing mutators without using the output object... however, it appears that in MATLAB i can't do this - the changes won't "stick" because of a scope issue?

%% Example Only
tripleAllPoints(obj1)
    obj1.pts = obj1.pts * 3;
end

For application purposes, an extremely simplified version of my code (which uses OO and recursion) is below.

classdef myslice

properties
    pts     % array of pts
    nROW % number of rows
    nDIM % number of dimensions
    subs    % sub-slices
end % end properties

methods
    function calcSubs(obj)
        obj.subs = cell(1,obj.nROW);
        for i=1:obj.nROW
            obj.subs{i} = myslice;
            obj.subs{i}.pts = obj.pts(1:i,2:end);
        end
    end

    function vol = calcVol(obj)
      if obj.nROW == 1
         obj.volume = prod(obj.pts);
      else
         obj.volume = 0;
         calcSubs(obj);
         for i=1:obj.nROW
                obj.volume = obj.volume + calcVol(obj.subs{i});
         end
      end
    end

end % end methods

end % end classdef

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-09-08 20:02:02

Matlab 有两种类型的类:句柄和值。

值类是按值传递的,因此每当写入时都必须复制它。此外,方法调用必须采用 obj = method(obj); 的形式,以便将更改“坚持”。

相反,句柄对象是通过引用传递的,因此,每当您在任何工作区(基础工作区或函数的工作区)中修改对象时,该对象都会在各处发生更改。因此,调用 method(obj); 也会更改调用工作区中的 obj,即使 obj 未显式返回。

默认类是值对象。如果您想使用句柄对象,您的 classdef 行必须是:

classdef myslice < handle

即您正在对句柄类进行子类化。

Matlab has two types of classes: handle and value.

A value class is passed by value, and has thus to be copied whenever you write to it. Also, method calls need to be of the form obj = method(obj); in order for the changes to 'stick'.

In contrast, handle objects are passed by reference, and thus, whenever you modify an object in any workspace - base workspace or a function's workspace - the object is changed everywhere. Thus, a call method(obj); changes obj in the calling workspace as well, even though obj is not returned explicitly.

The default class is the value object. If you want to use handle objects, your classdef line has to be:

classdef myslice < handle

i.e. you're subclassing the handle class.

z祗昰~ 2024-09-08 20:02:02

在这种情况下,您可以通过使用与输入相同的输出名称来向 MATLAB 提供有关正在发生的情况的额外提示。在您的示例中,这可以避免创建 obj 的副本。这可能并不总是合适(例如,如果您需要 obj.pts 的旧值和新值来更新某些其他属性)。

%% Example Only
obj = tripleAllPoints(obj)
    obj.pts = obj.pts * 3;
end

(另请参阅:http://blogs. mathworks.com/loren/2007/03/22/in-place-operations-on-data/

In this situation, you can give MATLAB an extra hint about what's going on by using the same name for your output as the input. In your example, this avoids creating a copy of obj. This may not always be appropriate (for example, if you need both the old and new values of obj.pts to update some other property).

%% Example Only
obj = tripleAllPoints(obj)
    obj.pts = obj.pts * 3;
end

(see also: http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/ )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文