Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Can you redefine an arithmetic operator in MATLAB?... Yes</p> <p><strong>Should</strong> you redefine an arithmetic operator in MATLAB?... Eh, probably not.</p> </blockquote> <p>Why? Because every other function in MATLAB expects that the arithmetic operator will behave as defined by the built-in implementation.</p> <p>I've answered a few other related questions that have dealt with overloading arithmetic operators and shadowing built-in behavior, which I definitely suggest reading through first to understand the details, difficulties, and pitfalls involved in such an approach:</p> <ul> <li><a href="https://stackoverflow.com/q/5365464/52738">MATLAB: Is it possible to overload operators on native constructs (cells, structs, etc)?</a></li> <li><a href="https://stackoverflow.com/q/2425251/52738">How do I get real integer overflows in MATLAB/Octave?</a></li> </ul> <p>And now that I'm done with my disclaimer, I'll hand you the gun with which to potentially shoot yourself in the foot... ;)</p> <hr> <p>Arithmetic operators in MATLAB have functional equivalents that are called behind the scenes when you invoke them, which are listed <a href="http://www.mathworks.com/help/matlab/arithmetic-operators.html" rel="nofollow noreferrer">here</a>. The arraywise power operator <code>.^</code> calls the built-in <a href="http://www.mathworks.com/help/matlab/ref/power.html" rel="nofollow noreferrer"><code>power</code></a> function when invoked.</p> <p>Now, there will be a separate <code>power</code> function defined for each <a href="https://www.mathworks.com/help/matlab/numeric-types.html" rel="nofollow noreferrer">data type</a> that uses it. This function will be placed in an <code>@type</code> directory, which you can see by using the <a href="https://www.mathworks.com/help/matlab/ref/which.html" rel="nofollow noreferrer"><code>which</code></a> function to look at the different <code>power</code> functions that exist:</p> <pre><code>&gt;&gt; which power -all built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@single\power) % single method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@double\power) % double method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@char\power) % char method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int64\power) % int64 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int32\power) % int32 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int16\power) % int16 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int8\power) % int8 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint64\power) % uint64 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint32\power) % uint32 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint16\power) % uint16 method built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint8\power) % uint8 method </code></pre> <p>If your variables <code>x</code> and <code>y</code> are going to be of type <a href="https://www.mathworks.com/help/matlab/ref/double.html" rel="nofollow noreferrer"><code>double</code></a> (as they are by default in MATLAB), then you will have to shadow the built-in <code>@double\power</code> function. You can do this by creating a directory (we'll call it <code>temp</code>), creating a subdirectory within that called <code>@double</code>, and then placing the following custom <code>power</code> function in that subdirectory:</p> <pre><code>function result = power(x, y) result = sign(x).*abs(builtin('power', x, y)); end </code></pre> <p>Now, based on the <a href="https://www.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html" rel="nofollow noreferrer">function precedence order</a> MATLAB follows, if you add the directory <code>temp</code> to the <a href="http://www.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html" rel="nofollow noreferrer">MATLAB path</a>, or if you simply change the <a href="http://www.mathworks.com/help/matlab/ref/cd.html" rel="nofollow noreferrer">current working directory</a> to <code>temp</code>, then the above custom <code>power</code> function will be called instead of the built-in one when using the <code>.^</code> operator on double variables.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload