5 views (last 30 days)

I am using element-wise power .^,which for A.^B should yield a matrix with elements A(i,j) raised to the power B(i,j). However, for the simple example:

A = [-1, 0; 0, -1];

B = [1, 0; 0, 1];

A.^B

I'm getting

ans =

-1.0000 + 0.0000i NaN + 0.0000i

NaN + 0.0000i -1.0000 + 0.0000i

If I use a loop, there is no error.

for i=1:2

for j =1:2

AB(i,j) = A(i,j)^B(i,j);

end

end

AB =

-1 1

1 -1

I can also select a single power coefficient and get the correct values, such as

A.^B(1,2)

ans =

1 1

1 1

What's wrong with the matrix element-wise power? Why is it giving out imaginary numbers and NaN for essentially 0^0, which should equal 1. Thanks.

EDIT: More Information on Replicating the Error

I tried to figure out why others can't replicate the errors I'm getting. After much frustration, I restarted the computer (Windows 10, R2018a) and tried it from a fresh instance, just copying and pasting the code into the Command Window, I get no error. However, if I run it from a script (e.g., script.m with the exact same code), then I get the error, and once it's errored, it can't be undone. Did some system settings change once I run it from an M file? I hope this helps to pinpoint the problem.

>> A = [-1, 0; 0, -1];

B = [1, 0; 0, 1];

A.^B

ans =

-1 1

1 -1

>> script

ans =

-1.0000 + 0.0000i NaN + 0.0000i

NaN + 0.0000i -1.0000 + 0.0000i

>> A = [-1, 0; 0, -1];

B = [1, 0; 0, 1];

A.^B

ans =

-1.0000 + 0.0000i NaN + 0.0000i

NaN + 0.0000i -1.0000 + 0.0000i

James Tursa
on 24 Jan 2020

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 10 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789397

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789397

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789402

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789402

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789579

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789579

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789697

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789697

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789706

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789706

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789748

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789748

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789752

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789752

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789753

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789753

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789756

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789756

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789820

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/501726-element-wise-power-resulting-in-imaginary-values-and-nan#comment_789820

Sign in to comment.