Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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_Class_Operator Overloading - Fatal编程技术网

Matlab 如何重载类的任意二进制或一元运算符?

Matlab 如何重载类的任意二进制或一元运算符?,matlab,class,operator-overloading,Matlab,Class,Operator Overloading,假设有一个类继承了数组x,添加了一些参数p: classdef test properties x p end methods function t=calculate(t) [t.x,t.p]=calc(x,p); end function t=plus(t1,t2) t.x=t1.x+t2.x; end end end 已知如何重载二

假设有一个类继承了数组
x
,添加了一些参数
p

classdef test
    properties
       x
       p
    end
    methods
       function t=calculate(t)
           [t.x,t.p]=calc(x,p);
       end
       function t=plus(t1,t2)
           t.x=t1.x+t2.x;
       end
   end
end

已知如何重载二进制运算符,例如
加号
mtimes
减号
,等等。。如何启用任何二进制向量化运算符的重载,或最终启用任何一元运算符,如
均值
abs
最大值
,以便直接应用于向量
x
?例如,我如何使
S=平均值(S)等效于
S.x=平均值(S.x)

如果我没弄错你的问题,听起来你希望你的新类
test
简单地继承为属性
x
类定义的所有二进制和一元方法(并在调用它们时对属性
x
进行操作),这样你就不必自己重新定义它们了

如果这是您想要的,那么我认为唯一可行的方法就是实际使用继承,并使您的类
测试
a属性
x
类。考虑到
x
只是一个简单的例子,可以找到一个将内置
double
类型子类化的好例子。根据您的示例,这里有一种实现类
测试的方法:

classdef test < double

  properties
    p
  end

  methods
    function obj = test(x, p)
      if (nargin < 2)
        p = 0;
        if (nargin < 1)
          x = 0;
        end
      end
      obj@double(x);
      obj.p = p;
    end

    function sref = subsref(obj, s)
      switch s(1).type
        case '.'
          switch s(1).subs
            case 'p'
              sref = obj.p;
            case 'x'
              x = double(obj);
              if (length(s) < 2)
                sref = x;
              elseif (length(s) > 1) && strcmp(s(2).type, '()')
                sref = subsref(x, s(2:end));
              end
            otherwise
              error('Not a supported indexing expression')
          end
        case '()'
          x = double(obj);
          newx = subsref(x, s(1:end));
          sref = test(newx, obj.p);
        case '{}'
          error('Not a supported indexing expression')
      end
    end

    function obj = subsasgn(obj, s, b)
      switch s(1).type
        case '.'
          switch s(1).subs
            case 'p'
              obj.p = b;
            case 'x'
              if (length(s) < 2)
                obj = test(b, obj.p);
              elseif (length(s) > 1) && strcmp(s(2).type, '()')
                x = double(obj);
                newx = subsasgn(x, s(2:end), b);
                obj = test(newx, obj.p);
              end
            otherwise
              error('Not a supported indexing expression')
          end
        case '()'
          x = double(obj);
          newx = subsasgn(x, s(1), b);
          obj = test(newx, obj.p);
        case '{}'
          error('Not a supported indexing expression')
      end
    end

    function disp(obj)
      fprintf('p:');
      disp(obj.p);
      fprintf('x:');
      disp(double(obj));
    end
  end

end

也许这篇类似的帖子可以帮助你:是的,我在写问题之前检查了一下。写
S0.oper(@plus,(S2.oper(@减号,(S0.oper(@abs⧖ЮЮ))
或者类似的东西来实现简单的算术运算,而不是
S0+S2 abs(S0)
…你提到了“继承”。这是否意味着
test
是包含
x
的类的子类?如果是这样,那么这个超类是否实现了任何内置操作符?如果
x
是一个实现了内置操作符的类,或者
x
是一个标准数组,我就保留这个问题。我们可以安全地考虑最后一个病例。
>> a = test(1:3, pi)  % Create an object with p = pi, and x = [1 2 3]

a = 
p:   3.141592653589793
x:     1     2     3

>> a.x = -a  % Unary operation on a, and reassignment to x

a = 
p:   3.141592653589793
x:    -1    -2    -3

>> a.x = a+4  % Binary operation and reassignment

a = 
p:   3.141592653589793
x:     3     2     1

>> a.x = mean(a)  % Another unary operation and reassignment

a = 
p:   3.141592653589793
x:     2