arrays - 类数组中的方法

标签 arrays matlab class

我有几个,其中有很多方法函数,通常处理mx1 数组。对于给定的:

S1.x=randn(m,1);
S1+2*randn(m,1); % Plus Override
S1.smooth; % Class Methods
S1.detrend; 

现在我希望以这样的方式处理相同给定classclassarrays

S=[S1 S2 S3 S4];
S.smooth; % Methods for Class Array
S.detrend;

问题: 有没有一种简单的方法可以做到这一点,而无需重写所有实现 属性方法函数 >?

我正在寻找一些特定的添加、重新定义、一段代码、技巧等,以便以干净的方式做到这一点。这样做的目的是代码功能,而不是性能 - 少数性能关键功能已经矢量化 -。

问候,

最佳答案

怎么样:

classdef TestClass
   methods
       function smooth(obj)
           if numel(obj) == 1
               disp('Hello')
           else
               for i=1:numel(obj)
                   obj(i).smooth;
               end
           end

       end
   end
end

调用方式如下:

>> t1 = TestClass;
>> t1.smooth
Hello


>> t2 = [TestClass TestClass TestClass];    
>> t2.smooth
Hello
Hello
Hello

如果你真的想要,你也应该能够重载 subsref运算符在您的所有方法上自动执行此操作(它使您可以访问 . 运算符)。然而,根据我的经验,正确重载 subsref 并不简单,而且可能比值得付出的努力更多。

这是这个想法的一个例子。它很简单,您可能需要进一步完善,但应该可以帮助您入门。请注意黑客的欢呼数量:)

classdef TestClass
properties
    Value
end

methods        
    function obj = TestClass(x)
        obj.Value = x;
    end

    function smooth(obj)
        fprintf('I am %d\n', obj.Value)
    end

    function res = opposite(obj)
        res = -obj.Value;
    end

    function [res1,res2,res3] = test(obj)
        res1 = obj.Value;
        res2 = res1*res1;
        res3 = res2*res1;
    end

    function varargout = subsref(A,S)
        if numel(A) > 1 && strcmp(S(1).type, '.')
            if nargout == 0
                feval(S.subs, A(1));
            else
                nout = nargout(['TestClass>TestClass.' S.subs]);
                if nout < 0
                    nout = -nout;
                end
                if nout == 0
                    arrayfun(@(x)feval(S.subs, x), A);
                    varargout = cell(1, nargout);
                else
                    for i=1:nargout                            
                        [output{1:nout}] = feval(S.subs, A(i));
                        varargout{i} = output;
                        output = {};
                    end
                end

            end
        else
            if nargout == 0
                builtin('subsref', A, S);
            else
                varargout{:} = builtin('subsref', A, S);
            end
        end
    end        
end

使用示例:

>> t1 = TestClass(5);
>> t1.smooth;
I am 5

>> t2 = [TestClass(1) TestClass(2) TestClass(3)];
>> t2(2).smooth;
I am 2
>> t2.smooth;
I am 1
I am 2
I am 3
>> t2(1:2).smooth
I am 1
I am 2
>> t2(2:3).smooth
I am 2
I am 3
>> t2([1 3]).smooth
I am 1
I am 3
>> t2.test

ans = 

    [1]    [1]    [1]


ans = 

    [2]    [4]    [8]


ans = 

    [3]    [9]    [27]

关于arrays - 类数组中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115610/

相关文章:

php - 如果 PHP 中的页面处于事件状态,则使用 foreach 显示事件类

java - 正确格式化数组列表数据的输出?

javascript - 如何循环一个对象并获取特定值以分别推送一个数组

c - 如何将二维数组传递给C中的函数?

init() setter 的 Ruby 类变量

api - 推特 API 限制

matlab - 透明重叠条形图

matlab - 在 MATLAB 中使用循环读取文件

image - 对图像进行机器学习时减少特征数量的方法

objective-c - 仅在参数更改时处理实例方法中的代码