How do I create efficient instance variable mutators in Matlab?

Posted by Trent B on Stack Overflow See other posts from Stack Overflow or by Trent B
Published on 2010-05-15T13:39:38Z Indexed on 2010/05/15 13:44 UTC
Read the original article Hit count: 302

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

© Stack Overflow or respective owner

Related posts about matlab

Related posts about this