Matlab类中的向量化

爱德华多

我在MATLAB中有一个代表虚数的类。我有一个构造函数和两个数据成员:realimag我在课堂上玩重载运算符,并且我想使其与矩阵一起使用:

function obj = plus(o1, o2)
   if (any(size(o1) ~= size(o2)))
      error('dimensions must match');
   end

   [n,m] = size(o1);
   obj(n,m) = mycomplex();
   for i=1:n
      for j=1:m
         obj(i,j).real = o1(i,j).real + o2(i,j).real;
         obj(i,j).imag = o1(i,j).imag + o2(i,j).imag;
      end
   end
end

但是我不想使用循环。我想做类似的事情:

[obj.real] = [o1.real] + [o2.real]

但是我不明白为什么它不起作用...错误说:

“输入错误+输出参数过多”。

我知道在MATLAB中最好避免for循环以加快速度...有人可以解释一下为什么这不起作用,以及以我的函数示例为例考虑MATLAB中矢量化的正确方法吗?

提前致谢。


编辑:我的复杂类的定义:

classdef mycomplex < handle & matlab.mixin.CustomDisplay
   properties (Access = public)
       real;
       imag;
   end

    methods (Access = public)
       function this = mycomplex(varargin)
           switch (nargin)
               case 0
                   this.real = 0;
                   this.imag = 0;
               case 1
                   this.real = varargin{1};
                   this.imag = 0;
               case 2
                   this.real = varargin{1};
                   this.imag = varargin{2};
               otherwise
                   error('Can''t have more than two arguments');
           end
           obj = this;
       end
    end
end
安姆罗

考虑下面的实现。首先要注意的是:

  • 构造函数可以不带任何参数调用。这对于允许预分配对象数组很重要obj(m,n) = MyComplex()

  • 为了方便起见,构造函数接受数组参数的任意一个标量。因此,我们可以致电:c_scalar = MyComplex(1,1)c_array = MyComplex(rand(3,1), rand(3,1))

  • plus运营商使用一个for循环,现在(我们稍后会改变这一点)。

(请注意,我跳过一些验证代码,比如检查o1o2大小都一样的,同样,对于ab在构造函数)。

classdef MyComplex < handle
    properties
        real
        imag
    end

    methods
        function obj = MyComplex(a,b)
            % default values
            if nargin < 2, b = 0; end
            if nargin < 1, a = 0; end

            % accepts scalar/array inputs
            if isscalar(a) && isscalar(b)
                obj.real = a;
                obj.imag = b;
            else
                [m,n] = size(a);
                obj(m,n) = MyComplex(); 
                for i=1:m*n
                    obj(i).real = a(i);
                    obj(i).imag = b(i);
                end
            end
        end

        function obj = plus(o1, o2)
            [m,n] = size(o1);
            obj(m,n) = MyComplex();  % preallocate object array
            for i=1:m*n              % linear indexing
                obj(i).real = o1(i).real + o2(i).real;
                obj(i).imag = o1(i).imag + o2(i).imag;
            end
        end
    end
end

使用该类的示例:

% scalar objects
>> c1 = MyComplex(1,2);
>> c2 = MyComplex(3,4); 
>> c3 = c1 + c2
c3 = 
  MyComplex with properties:

    real: 4
    imag: 6

% array of objects
>> c4 = [c1;c1] + [c2;c2]
c4 = 
  2x1 MyComplex array with properties:

    real
    imag

现在是该plus方法的向量化版本

function obj = plus(o1, o2)
    [m,n] = size(o1);
    obj(m,n) = MyComplex();

    x = num2cell([o1.real] + [o2.real]);
    [obj.real] = deal(x{:});

    x = num2cell([o1.imag] + [o2.imag]);
    [obj.imag] = deal(x{:});
end

我使用的语法是:[objarray.propName]引用对象数组中的属性,这会将值作为向量返回。

与在对象数组中分配属性相反,我使用逗号分隔的列表,因此我必须转换为单元格数组才能获得x{:}方便的语法。

请注意,deal并非严格要求调用,我们可以在没有调用的情况下编写分配:

[obj.real] = x{:};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章