Arrays Matlab中的面向对象编程:为具有负索引的2D阵列创建新类
我不熟悉面向对象编程。这个问题似乎类似于以前提出的问题,但有一个很大的区别,我还没有找到解决办法: 如何使用Matlab中的面向对象编程设置一类新的变量,该类变量的行为类似于矩阵(2D数组…),但行/列的编号为{-2,-1,0,1…}(而不是仅{1,2,3…})Arrays Matlab中的面向对象编程:为具有负索引的2D阵列创建新类,arrays,matlab,matrix,object-oriented-database,Arrays,Matlab,Matrix,Object Oriented Database,我不熟悉面向对象编程。这个问题似乎类似于以前提出的问题,但有一个很大的区别,我还没有找到解决办法: 如何使用Matlab中的面向对象编程设置一类新的变量,该类变量的行为类似于矩阵(2D数组…),但行/列的编号为{-2,-1,0,1…}(而不是仅{1,2,3…}) 我希望能够在这个新类中使用Matlab提供的所有正则向量运算。处理此变量中的值的方法是什么(例如,A[-1,2]=…与普通数组类似?A.-1.2=…?)?我将如何执行简单的A.*B(Matlab中向量的逐元素乘法)?和A(:,5)。*B
我希望能够在这个新类中使用Matlab提供的所有正则向量运算。处理此变量中的值的方法是什么(例如,A[-1,2]=…与普通数组类似?A.-1.2=…?)?我将如何执行简单的A.*B(Matlab中向量的逐元素乘法)?和A(:,5)。*B(-1,:)' 您可以对内置的
double
类进行子类化,该类将为您提供大部分功能。关于索引,您只需编写自己的subsref
和subsasgn
方法
由于您已经将double
子类化,所有正常的矩阵运算(如元素相乘、cumsum
等)都将继续按预期工作
这样的办法应该行得通
classdef mymatrix < double
methods
function self = mymatrix(varargin)
% Constructor that simply calls the double constructor
self@double(varargin{:});
end
function res = subsref(self, subs)
% Call the usual subsref after modifying the subscripts
res = subsref@double(self, correctsubs(self, subs));
end
function res = subsasgn(self, subs, val)
% Call the usual subsasgn after modifying the subscripts
res = subsasgn@double(self, correctsubs(self, subs), val);
end
end
methods (Access = 'private')
function subs = correctsubs(self, subs)
% Function for converting subscripts
for k = 1:numel(subs)
% Only process () references and non-linear indices
if ~isequal(subs(k).type, '()')
continue
end
% Figure out the center of the matrix
mid = ceil(size(self) / 2);
% For each subscript, if it's numeric then add the middle
% shift amount to it to turn it into a normal subscript
for m = 1:numel(subs(k).subs)
if isnumeric(subs(k).subs{m})
subs(k).subs{m} = subs(k).subs{m} + mid(m);
end
end
end
end
end
end
您可以对内置
double
类进行子类化,该类将为您提供大部分功能。关于索引,您只需编写自己的subsref
和subsasgn
方法
由于您已经将double
子类化,所有正常的矩阵运算(如元素相乘、cumsum
等)都将继续按预期工作
这样的办法应该行得通
classdef mymatrix < double
methods
function self = mymatrix(varargin)
% Constructor that simply calls the double constructor
self@double(varargin{:});
end
function res = subsref(self, subs)
% Call the usual subsref after modifying the subscripts
res = subsref@double(self, correctsubs(self, subs));
end
function res = subsasgn(self, subs, val)
% Call the usual subsasgn after modifying the subscripts
res = subsasgn@double(self, correctsubs(self, subs), val);
end
end
methods (Access = 'private')
function subs = correctsubs(self, subs)
% Function for converting subscripts
for k = 1:numel(subs)
% Only process () references and non-linear indices
if ~isequal(subs(k).type, '()')
continue
end
% Figure out the center of the matrix
mid = ceil(size(self) / 2);
% For each subscript, if it's numeric then add the middle
% shift amount to it to turn it into a normal subscript
for m = 1:numel(subs(k).subs)
if isnumeric(subs(k).subs{m})
subs(k).subs{m} = subs(k).subs{m} + mid(m);
end
end
end
end
end
end
请参阅:请参阅:@user1611107更新为
subsasgn
aswell@user1611107更新时还修改了subsasgn