MATLAB: Is it possible to overload operators on native constructs (cells, structs, etc)?
Solution 1:
It is in fact possible to create new operators or overload existing ones for built-in data types in MATLAB. I describe one example of this in my answer to another SO question about modifying the default overflow behavior of integer types.
First, you may want to look at what methods currently exist for cell arrays. You can do this using the function METHODS, and here's what I get in MATLAB R2010b:
>> methods cell
Methods for class cell:
aa2nt issorted regexptranslate strfind
accumarray newdepfun reshape strjust
cell2struct nt2aa rna2dna strmatch
ctranspose nwalign seq2regexp strtok
display permute setdiff transpose
dna2rna regexp setxor union
intersect regexpi sort unique
ismember regexprep strcat
The arithmetic operator methods would show up in the above list as their function equivalents, like plus
for the +
operator or times
for the .*
operator. Only the transpose
method (.'
operator) is defined for cell arrays. You would have to create the rest yourself, defining how a given operator will behave for cell arrays arguments.
You can do this by first making a new folder called @cell
and placing it in an existing folder on your MATLAB path. You would then place your new methods in the @cell
folder. For example, a very simple implementation of a plus
method for cell arrays (without any input-checking, error-checking, etc.) would be this:
function C = plus(A,B)
C = cellfun(@plus,A,B,'UniformOutput',false); %# Apply plus cell-wise
end
In the above code, you would probably first want to check that the operands A
and B
are cell arrays of the same size. However, you could create whatever unique functionality you want, such as allowing B
to be a scalar value which would get added to every cell of A
. It's totally up to you to define how the +
operator will behave for cell arrays.
This would then allow you to write your code in a much more compact way, as in this example:
>> A = {[1 2 3] [4 5] 6}; %# One 3-element cell array
>> B = {5 [4 5] 2}; %# Another 3-element cell array
>> C = A+B; %# Use the new plus operator
>> C{:} %# Display the cell contents
ans =
6 7 8
ans =
8 10
ans =
8
I can't really speak to the behind-the-scenes optimizations and how this might affect them. I know that the documentation "Techniques for Improving Performance" specifically mentions this about overloading built-in functions:
Overloading MATLAB built-in functions on any of the standard MATLAB data classes can negatively affect performance. For example, if you overload the
plus
function to handle any of the integer classes differently, you may hinder certain optimizations in the MATLAB built-in function code forplus
, and thus may slow down any programs that make use of this overload.
However, in your case you aren't overloading existing functions for a class. You're simply creating new ones that didn't exist for that class, so it's hard to say what effect this may ultimately have on performance.