虚数を表す MATLAB のクラスがあります。コンストラクターと 2 つのデータ メンバーがあります:real
とimag
. クラスのオーバーロード演算子で遊んでいて、行列で動作させたい:
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
しかし、for ループは使いたくありません。私は次のようなことをしたい:
[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