Don't. It won't be correct. (-1).^(1/2) should always give you the imaginary unit (i). The expression you have would give you (-1).^(1/2) -> -1. Even worse, consider (-1)^2.
Create a separate function to perform the operation you're describing. Something like
function a = myPowerFunc(x, y)
a = sign(x).*abs(x.^y);
Can you redefine an arithmetic operator in MATLAB?... Yes
Should you redefine an arithmetic operator in MATLAB?... Eh, probably not.
Why? Because every other function in MATLAB expects that the arithmetic operator will behave as defined by the built-in implementation.
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:
And now that I'm done with my disclaimer, I'll hand you the gun with which to potentially shoot yourself in the foot... ;)
Arithmetic operators in MATLAB have functional equivalents that are called behind the scenes when you invoke them, which are listed here. The arraywise power operator .^ calls the built-in power function when invoked.
Now, there will be a separate power function defined for each data type that uses it. This function will be placed in an @type directory, which you can see by using the which function to look at the different power functions that exist:
If your variables x and y are going to be of type double (as they are by default in MATLAB), then you will have to shadow the built-in @double\power function. You can do this by creating a directory (we'll call it temp), creating a subdirectory within that called @double, and then placing the following custom power function in that subdirectory:
function result = power(x, y)
result = sign(x).*abs(builtin('power', x, y));
end
Now, based on the function precedence order MATLAB follows, if you add the directory temp to the MATLAB path, or if you simply change the current working directory to temp, then the above custom power function will be called instead of the built-in one when using the .^ operator on double variables.
Don't. It won't be correct. (-1).^(1/2) should always give you the imaginary unit (i). The expression you have would give you (-1).^(1/2) -> -1. Even worse, consider (-1)^2.
Create a separate function to perform the operation you're describing. Something like
Why? Because every other function in MATLAB expects that the arithmetic operator will behave as defined by the built-in implementation.
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:
And now that I'm done with my disclaimer, I'll hand you the gun with which to potentially shoot yourself in the foot... ;)
Arithmetic operators in MATLAB have functional equivalents that are called behind the scenes when you invoke them, which are listed here. The arraywise power operator
.^
calls the built-inpower
function when invoked.Now, there will be a separate
power
function defined for each data type that uses it. This function will be placed in an@type
directory, which you can see by using thewhich
function to look at the differentpower
functions that exist:If your variables
x
andy
are going to be of typedouble
(as they are by default in MATLAB), then you will have to shadow the built-in@double\power
function. You can do this by creating a directory (we'll call ittemp
), creating a subdirectory within that called@double
, and then placing the following custompower
function in that subdirectory:Now, based on the function precedence order MATLAB follows, if you add the directory
temp
to the MATLAB path, or if you simply change the current working directory totemp
, then the above custompower
function will be called instead of the built-in one when using the.^
operator on double variables.