Element-wise power resulting in imaginary values and NAN

조회 수: 5 (최근 30일)
MathN00b
MathN00b 2020년 1월 23일
댓글: Walter Roberson 2020년 1월 25일
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
  댓글 수: 10
Walter Roberson
Walter Roberson 2020년 1월 24일
Could you attach the script that is triggering the problem?
MathN00b
MathN00b 2020년 1월 25일
Thank you for your help, Walter! The script contains the exact same code
A = [-1, 0; 0, -1];
B = [1, 0; 0, 1];
A.^B
I figured out the problem, thanks to James. One of the libraries contained a directory named @double with an alternative power.m file, which overloaded the default power function. I had no idea this is called when I used .^. It defined
o=power(s,t)
as
o=exp(log(s)*t);
or
o=exp(log(s).*t);
depending on the arguments. Thanks again for your patience and help, I really appreciate it.

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2020년 1월 24일
Looks like the problem calculation is being done in the background as exp(B.*log(A)), but I don't know why it does this sometimes and not other times.
  댓글 수: 2
MathN00b
MathN00b 2020년 1월 25일
Thanks, James! One of the libraries contained a directory named @double with an alternative power.m file, which overloaded the default power function. I had no idea this is called when I used .^. It defined
o=power(s,t)
as
o=exp(log(s)*t);
or
o=exp(log(s).*t);
depending on the arguments. Much thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by